Questions tagged [ascription]

Type ascriptions are annotations constraining the type inference mechanism.

Type ascriptions allow developers to set constraint on compiler type inference. For instance in Scala language:

val i = 123         //Inferred as Int
val l: Long = 123   //Inferred as Long

Type ascriptions allow to force implicit conversions and to pass a sequence to a method expecting variable arguments. For more information, see the question: What is the purpose of type ascription in Scala?

7 questions
77
votes
5 answers

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…
davetron5000
  • 21,658
  • 10
  • 64
  • 97
30
votes
1 answer

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…
Angel Angel
  • 13,274
  • 20
  • 64
  • 96
29
votes
3 answers

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)…
Tom Crockett
  • 28,680
  • 6
  • 68
  • 86
5
votes
2 answers

Why does flatten on nested Iterator not compile and why do I need type ascription?

(new Iterator[List[Int]] { def hasNext: Boolean = ??? def next(): List[Int] = ??? }).flatten gives error: value flatten is not a member of Iterator[List[Int]] [error] possible cause: maybe a semicolon is missing before `value flatten'? [error] …
4
votes
1 answer

Autocasting types using 'value: Type' syntax

When I am reading Scala reflection tutorial. I found a syntax very wired as follows. import scala.reflect.runtime.universe._ typeOf[List[_]].member("map": TermName) So the member function takes Name type parameter, and then "map": TermName is…
kevin
  • 664
  • 6
  • 13
1
vote
2 answers

Functor Structure extension and Multiple Ascription in SML

Is there any way in Standard ML to make a functor output a structure which has all of the functionality of the passed in structure, plus any new functionality. In a similar way, is it possible to do multiple ascription? In the case of the above it…
Daniel Holden
  • 59
  • 1
  • 3
0
votes
3 answers

Force grouping of ascription on underscore in scala

I am trying to do: MyObject.myMethod(_:MyType.myAttribute) This fails with type myAttribute is not a member of object MyObject which is correct. The problem is that I want to call myMethod on myAttribute of _:MyType, not ascribe MyType:myAttribute…
taz
  • 1,366
  • 1
  • 13
  • 25