I’m playing around with dotty/scala-3 for Advent of Code, and enabled the -Yexplicit-nulls
feature. As I don’t use any external Java libraries, I expected this to only cause errors, when there is something actually wrong, but it turns out, Java’s standard library doesn’t have @NonNull
annotations. Scala uses java.lang.String
for Strings, so this causes problems, when calling methods on them:
scala> val a: String = "foo\n"
val a: String = "foo
"
scala> val b: String = a.trim
1 |val b: String = a.trim
| ^^^^^^
| Found: String | UncheckedNull
| Required: String
Is there a way to use external annotations for the standard library, so the compiler can know which Java methods won’t return null? I know that JetBrains provides these for the inspections in their IDE, but can these also be used in the compiler? Or is the only way currently to have superfluous checks or asInstanceOf
calls?