Also, why does it even allow this:
null.asInstanceOf[Int]
knowing that we can't do this:
val x: Int = null
Grantedly, Scala shouldn't always be modeled by Java's behaviour, but in
this particular case the Java way of doing things is better because it
is more typesafe.
So what's the alternative for people who are using the null.asInstanceOf[T] technique to get default values?
--
You received this message because you are subscribed to the Google Groups "scala-language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-languag...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
To provide a separate mechanism.
This email message and any attachments are intended solely for the use of the addressee. If you are not the intended recipient, you are prohibited from reading, disclosing, reproducing, distributing, disseminating or otherwise using this transmission. If you have received this message in error, please promptly notify the sender by reply email and immediately delete this message from your system. This message and any attachments may contain information that is confidential, privileged or exempt from disclosure. Delivery of this message to any person other than the intended recipient is not intended to waive any right or privilege. Message transmission is not guaranteed to be secure or free of software viruses.
***********************************************************************************************************************
Hello,
scala> null.asInstanceOf[Int]
res8: Int = 0
scala> 0:Any
res9: Any = 0
scala> null.asInstanceOf[Int]:Any
res10: Any = null
Essentially, the compiler unboxes null to default value, and may optimize away subsequent unbox/box operations even though it will not give the same result for null.
Best, Oliver
--
You received this message because you are subscribed to the Google Groups "scala-language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
scala-languag...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
So what's the alternative for people who are using the null.asInstanceOf[T] technique to get default values?
--
The reason why the cast is allowed is the type checker treats asInstanceOf as a normal method defined in the Any type.
--
Following the current logic, null.isInstanceOf[AnyVal] should always
yield true, because we can always cast to a default value. However, the
spec says
(http://www.scala-lang.org/files/archive/spec/2.11/06-expressions.html#the-null-value):
"isInstanceOf[T] always returns false"
Shouldn't isInstanceOf and asInstanceOf obey this rule?
When v.isInstanceOf[T] returns false, then asInstanceOf[T] should throw
a ClassCastException.
--
Ionuț G. Stan | http://igstan.ro
--
You received this message because you are subscribed to the Google Groups "scala-language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-language+unsubscribe@googlegroups.com.
Best, Oliver
--
Ionuț G. Stan | http://igstan.ro
--
You received this message because you are subscribed to the Google Groups "scala-language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-language+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
--
You received this message because you are subscribed to the Google Groups "scala-language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-languag...@googlegroups.com.