When using the new Scala 3's flag -Yexplicit-nulls
, every Java code which doesn't have explicit non-null annotations is treated as nullable, thus every Java method not returning a primitive type T
effectively returns T | Null
.
Problem: In Scala, String is just an alias for java.lang.String
. And because core Java doesn't contain any annotations about the nullability, Scala 3 treats the return types of String methods as nullable.
Example: (tried via both Scala REPL / sbt console, and with Gradle)
scala> val bla: String = null // check whether the compiler option is turned on
-- Error:
1 |val bla: String = null
| ^^^^
| Found: Null
| Required: String
scala> val bla: String = "hey"
val bla: String = hey
scala> bla.split("e")
val res0: Array[String | Null] | Null = Array(h, y)
The return type Array[String | Null] | Null
is completely insane. The contract of the split
method is obviously given as it always returns an array and that array always contains at least one string (and none of the strings is null).
Is this really the sad reality, or am I doing something wrong?
To make the above work, I would always have to write:
"hey".split("e").nn.map(_.nn) // this would finally give me just Array[String]
bad option '-Yjava-interop-checker-framework' was ignored
– Vitrescence