天道酬勤,学无止境

ascription

Why can't the type of id be specialised to (forall a. a -> a) -> (forall b. b -> b)?

Take the humble identity function in Haskell, id :: forall a. a -> a Given that Haskell supposedly supports impredicative polymorphism, it seems reasonable that I should be able to "restrict" id to the type (forall a. a -> a) -> (forall b. b -> b) via type ascription. But this doesn't work: Prelude> id :: (forall a. a -> a) -> (forall b. b -> b) <interactive>:1:1: Couldn't match expected type `b -> b' with actual type `forall a. a -> a' Expected type: (forall a. a -> a) -> b -> b Actual type: (forall a. a -> a) -> forall a. a -> a In the expression: id :: (forall a. a -> a) -> (forall b. b ->

2021-05-16 18:50:01    分类:问答    haskell   polymorphism   impredicativetypes   ascription

什么是类型归属?(What is type ascription?)

问题 几次我使用了错误的语法,例如在此示例中忘记使用let : let closure_annotated = |value: i32| -> i32 { temp: i32 = fun(5i32); temp + value + 1 }; error[E0658]: type ascription is experimental (see issue #23416) --> src/main.rs:3:9 | 3 | temp: i32 = fun(5i32); | ^^^^^^^^^ 我知道可以通过使用let解决此问题,但是什么是“类型归因”,它的用途是什么? 我发现了问题#23416和类型归因的功能门,但是我不明白什么是“类型归因”或它的目的是什么。 回答1 类型说明是一种用我们希望它具有的类型来注释表达式的能力。 RFC 803中描述了Rust中的类型归因。 在某些情况下,表达式的类型可能不明确。 例如,此代码: fn main() { println!("{:?}", "hello".chars().collect()); } 给出以下错误: error[E0283]: type annotations required: cannot resolve `_: std::iter::FromIterator<char>` --> src/main.rs:2:38 | 2

2021-05-09 04:29:56    分类:技术分享    syntax   rust   ascription

What is type ascription?

Several times I've used the wrong syntax, such as forgetting to use let in this example: let closure_annotated = |value: i32| -> i32 { temp: i32 = fun(5i32); temp + value + 1 }; error[E0658]: type ascription is experimental (see issue #23416) --> src/main.rs:3:9 | 3 | temp: i32 = fun(5i32); | ^^^^^^^^^ I know that this problem is solved by using let, but what is "type ascription" and what is its use? I found issue #23416 and the feature gate for type ascription, but I could not understand what "type ascription" is or what is its purpose.

2021-04-09 17:56:00    分类:问答    syntax   rust   ascription

Scala中的类型归属的目的是什么?(What is the purpose of type ascriptions in Scala?)

问题 规范中没有太多有关类型归属的信息,并且当然没有关于目的的任何信息。 除了“使传递的varargs起作用”之外,我还要使用类型归属吗? 以下是一些scala REPL,以了解使用它的语法和效果。 scala> val s = "Dave" s: java.lang.String = Dave scala> val p = s:Object p: java.lang.Object = Dave scala> p.length <console>:7: error: value length is not a member of java.lang.Object p.length ^ scala> p.getClass res10: java.lang.Class[_ <: java.lang.Object] = class java.lang.String scala> s.getClass res11: java.lang.Class[_ <: java.lang.Object] = class java.lang.String scala> p.asInstanceOf[String].length res9: Int = 4 回答1 类型说明只是告诉编译器,从所有可能的有效类型中,表达式期望什么类型。 如果类型遵守现有的约束(例如方差和类型声明),则它是有效的

2021-03-28 23:41:48    分类:技术分享    scala   static-typing   ascription

What is the purpose of type ascriptions in Scala?

There's not much info in the spec on what type ascription is, and there certainly isn't anything in there about the purpose for it. Other than "making passing varargs work", what would I use type ascription for? Below is some scala REPL for the syntax and effects of using it. scala> val s = "Dave" s: java.lang.String = Dave scala> val p = s:Object p: java.lang.Object = Dave scala> p.length <console>:7: error: value length is not a member of java.lang.Object p.length ^ scala> p.getClass res10: java.lang.Class[_ <: java.lang.Object] = class java.lang.String scala> s.getClass res11: java.lang

2021-03-23 09:51:49    分类:问答    scala   static-typing   ascription