Confusing error msg when assigning null to variables of value classes

1,249 views
Skip to first unread message

Bjorn Regnell

unread,
May 3, 2016, 1:22:47 PM5/3/16
to scala-user
I'm working on some exercises for programming beginners to help them understand the differences between reference classes and value classes (also relevant in a context of comparing Java and Scala) and I discovered this potentially confusing error message:

$ scala
Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_66).
Type in expressions for evaluation. Or try :help.

scala> val i: Int = null
<console>:11: error: an expression of type Null is ineligible for implicit conversion
       val i: Int = null

Why does the compiler tell me this?

Would it be possible to tell something about that AnyVal values can't be null or similar?

(
Some related messages that perhaps is not as confusing as the above, but perhaps also confusing for a beginner (although they might probably be comforted when they have understood the Scala type hierarchy):

scala> class MyVal(val v: Int) extends AnyVal
defined class MyVal

scala> val mv: MyVal = null
<console>:13: error: type mismatch;
 found   : Null(null)
 required: MyVal
       val mv: MyVal = null
                       ^
Furthermore: A note that might be rather confusing:

scala> val av: AnyVal = null
<console>:11: error: type mismatch;
 found   : Null(null)
 required: AnyVal
Note that implicit conversions are not applicable because they are ambiguous:
 both method RichException in object Predef of type (self: Throwable)RichException
 and method augmentString in object Predef of type (x: String)scala.collection.immutable.StringOps
 are possible conversion functions from Null(null) to AnyVal
       val av: AnyVal = null
                        ^
)

Oliver Ruebenacker

unread,
May 3, 2016, 1:38:58 PM5/3/16
to Bjorn Regnell, scala-user

     Hello,

  You don't need to know that Int is an AnyVal to understand that null cannot be converted to Int, right?

  It should probably just say "cannot convert null to Int".

     Best, Oliver

--
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/d/optout.



--
Oliver Ruebenacker
Senior Software Engineer, Diabetes Portal, Broad Institute

Björn Regnell

unread,
May 3, 2016, 2:22:11 PM5/3/16
to Oliver Ruebenacker, scala-user

> "cannot convert null to Int".

 

Yes that msg would be nicer, but perhaps still a bit strange as one might wonder why it should try to convert at all (esp when assigning null to a reference type doesn't imply conversion). Why try to convert anyway, when it is just a semantic issue that value classes can't be null?

 

Anyway, beginners will have difficulties with error messages at the beginning anyway so this might not be more difficult than many other error messages. I guess it is rather difficult to establish a consistent terminology across all error messages...

 

Is it worth it to file an issue on the error message, do you think?

 

Thanks,

/B

Oliver Ruebenacker

unread,
May 3, 2016, 2:38:16 PM5/3/16
to Björn Regnell, scala-user

     Hello,

  On second thought, the error message is not as bad as I thought.

  The point is not so much that an Int cannot be null. For example, the following is perfectly valid Scala, if I have an implicit conversion from String to Int in scope:

  val i: Int = "fourty-two"

  The point is that there never can be an implicit conversion from Null to Int, because implicit conversion from Null is explicitly forbidden.

  Therefore, I am not sure any more that my suggestion would be any better.

     Best, Oliver

Björn Regnell

unread,
May 3, 2016, 3:15:47 PM5/3/16
to Oliver Ruebenacker, scala-user

Hi Oliver

Many thanks for your help!

Is it false to say that "AnyVal values cannot be null"?

 

> The point is that there never can be an implicit conversion from Null to Int

 

My argument is about regularity, I guess. What I meant was that in other situations the compiler does not seem to consider conversions, as error messages are simply "type mismatch" even if there in theory *could* be a conversion, e.g.. in this REPL-interaction (rather untypical, but for the sake of example):

 

Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_66).

Type in expressions for evaluation. Or try :help.

 

scala> class MyRefClass

defined class MyRefClass

 

scala> val r: MyRefClass = null

r: MyRefClass = null

 

scala> val i: MyRefClass = 42

<console>:12: error: type mismatch;

found   : Int(42)

required: MyRefClass

       val i: MyRefClass = 42

                           ^

 

scala> implicit def intToMyRefClass(i: Int) = new MyRefClass

warning: there was one feature warning; re-run with -feature for details

intToMyRefClass: (i: Int)MyRefClass

 

scala> val i: MyRefClass = 42

i: MyRefClass = MyRefClass@6f195bc3

Reply all
Reply to author
Forward
0 new messages