I accidentally noticed the following:
scala> class g[T](val x:T)
defined class g
scala> val obj=new g[Int]('A')
obj: g[Int] = g@1082d45
scala> obj.x
res6: Int = 65
What is happening here? A typecast?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It’s upconversion of primitives. Primitive values will convert themselves to the next numerically-larger type if it seems like that is needed. So
val s: Short = (0: Byte)works. Nothing converts toChar, butCharwill become anInt,Long,Float, orDoubleif it needs to to be the right argument type.The reason this is done is mostly because it’s how Java does it, but partly because getting an error off of
val d: Double = 0is really annoying since0of course fits into aDoublewith no problems whatsoever, and there’s no ambiguity about the type either.