|
[#942] |
project: compiler |
priority: low |
category: bug |
|
submitter |
assigned to |
status |
date submitted |
|
Nikolay |
Martin |
open |
2007-02-11 17:24:21.0 |
subject |
[contrib #326] null : Scala.null(null) which is not <: |
code |
// in the scala 2.3.3 interpreter:
// "bind" for Option using null for None and everything else for Some
scala> def maybe[t <: AnyRef, s >: Null <: AnyRef ](x: t)(f: t => s): s = if(x==null) null else f(x)
maybe: [t <: java.lang.Object,s >: scala.Null <: java.lang.Object](t)((t) => s)s
scala>
scala> maybe("foo")((x:String) => x.length)
<console>:5: error: type mismatch;
found : () => scala.Int
required: scala.Null
val line1 = maybe("foo")((x:String) => x.length)
^
scala> maybe("foo")(x => new Integer(x.length))
<console>:5: error: type mismatch;
found : java.lang.Integer
required: scala.Null
val line2 = maybe("foo")(x => new Integer(x.length))
^
scala> maybe("foo")((x: String) => new Integer(x.length))
<console>:5: error: type mismatch;
found : java.lang.Integer
required: scala.Null
val line3 = maybe("foo")((x: String) => new Integer(x.length))
^
scala> maybe[String,Integer]("foo")(x => new Integer(x.length))
line4: java.lang.Integer = 3 |
what happened |
none of the error messages make sense to me
I understand that Int's are not nullable, so the "obvious" first attempt doesn't work (although I guess they cou\
ld be autoboxed?)
However, the next two error messages seem to indicate something goes wrong with inferencing type parameters.
|
what expected |
Better error messages. (Maybe better inferencing, even more maybe: autoboxing?)
ps: This null business is of course a big no-no in pure Scala code, but when interfacing with Java code, you don't always want to manually wrap everything to use Option |
[back to overview] |