Why can I cast null to String, but not Int?

430 views
Skip to first unread message

Oliver Ruebenacker

unread,
Feb 8, 2013, 10:05:17 AM2/8/13
to scala-user
Hello,

Why is casting null to String any different from casting null to Int?

scala> null
res11: Null = null

scala> null : String
res12: String = null

scala> null : Int
<console>:8: error: type mismatch;
found : Null(null)
required: Int
Note that implicit conversions are not applicable because they are ambiguous:
both method Integer2intNullConflict in class LowPriorityImplicits of
type (x: Null)Int
and method Integer2int in object Predef of type (x: Integer)Int
are possible conversion functions from Null(null) to Int
null : Int
^

Thanks!

Take care
Oliver

--
IT Project Lead at PanGenX (http://www.pangenx.com)
The purpose is always improvement

Peter Salanki

unread,
Feb 8, 2013, 10:09:46 AM2/8/13
to Oliver Ruebenacker, scala-user
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



Oliver Ruebenacker

unread,
Feb 8, 2013, 10:30:50 AM2/8/13
to scala-user
Oh, I got it. null is of type Null, which is sub-class of AnyRef, but
not AnyVal.
Reply all
Reply to author
Forward
0 new messages