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.Class[_ <: java.lang.Object] = class java.lang.String
scala> p.asInstanceOf[String].length
res9: Int = 4
One possibility is when network and serial protocol level stuff, then this:
is far cleaner than
The second form is also a runtime conversion (not handled by the compiler) and could lead to some interesting over/underflow conditions.
You may find this thread illuminating, if a bit convoluted to follow. The important thing to note is that you're adding constraint hints to the type checker - it gives you a little more control over what that compilation phase is doing.
Type Inference: We can skip Explicitly giving Name of Type of Something in source code, called Type Inference.(Although required in some exceptional cases.)
Type Ascription: Being explicit about the type of something is called a Type Ascription. What Difference It can make?
ex: val x = 2 : Byte
also see: 1. We can explicitly give return type to our functions
Another way of declaring this could be:
Here we did not give
Option[Option[String]]
return type explicitly and Compiler inferred it asSome[Option[String]]
. WhySome[Option[String]]
is because we used type ascription in the definition.Another way we can use the same definition is:
def t3 = Some(None)
> t3: Some[None.type]
This time We did not explicitly tell the compiler anything(neither this defi). And It inferred our definition as Some[None.type]
Type ascription is just telling the compiler what type you expect out of an expression, from all possible valid types.
A type is valid if it respects existing constraints, such as variance and type declarations, and it is either one of the types the expression it applies to "is a", or there's a conversion that applies in scope.
So,
java.lang.String extends java.lang.Object
, therefore anyString
is also anObject
. In your example you declared you want the expressions
to be treated as anObject
, not aString
. Since there is no constraints preventing that and the desired type is one of the typess
is a, it works.Now, why would you want that? Consider this:
You could also have fixed this by type ascripting
s
atss
declaration, or you could have declaredss
's type to beSet[AnyRef]
.However, type declarations achieve the same thing only as long as you are assigning a value to an identifier. Which one can always do, of course, if one doesn't care about littering the code with one-shot identifiers. For example, the following does not compile:
But this does:
It would be silly to use an identifier here in place of
Nil
. And though I could just writeList[String]()
instead, that isn't always an option. Consider this, for instance:For the reference, this is what Scala 2.7 spec (march 15, 2009 draft) has to say about type ascription:
I use type ascription to paper over holes in Scala's type inference. For example, foldLeft over a collection of type A takes an initial element of type B and a function (B, A) => B that is used to fold the elements of the collection into the initial element. The actual value of type B is inferred from the type of the initial element. Since Nil extends List[Nothing], using it as an initial element causes problems:
Alternatively, you could just use List.empty[Int] instead of Nil:List[Int].
edit: List.empty[A] is implemented as
(source)
This is effectively a more verbose form of Nil:List[A]