scala with explicit nulls

Abel Nieto edited this page Dec 7, 2018 · 8 revisions

project logo

Scala with explicit nulls

This is a version of the Dotty compiler where Scala reference types are non-nullable by default.

This means that val x: String = null no longer type-checks. Instead, you can use union types to get nullability back: val x: String|Null = null.

Features

✅ = done | 🔶 = in progress | 🔴 = planned

🔶 Initial version

  • Null is not a subtype of reference types
    val x: String = null // no longer compiles
    
  • ✅ Nullability is expressible via union types
    val x: String|Null = null // ok
    
  • ✅ Scala "nullifies" types that come from Java (argument types and method argument and return types)
    val x = "abc".substring(2) // x has type String|JavaNull    
    
    nullify(T)    -> T|JavaNull             (if T is not generic)
    nullify(C[T]) -> C[nullify(T)]|JavaNull (if T is generic)
    
  • ✅ The JavaNull type is "transparent" w.r.t field selections:
    val x: Person = ??? // Person is defined in Java
    val y = x.getName() // inferred type is String|JavaNull
    y.length() // type-checks, because JavaNull is "transparent"
               // can also throw NPE
    
  • ✅ Basic flow-sensitive type inference
    val x: String|Null = ???
    if (x != null) {
      // x has type String here
    }
    
    Also propagated inside the condition
    if (x != null && x.length > 0) {} // x.length types, even though x declared as String|Null
    
    We also support ||s
    if (x == null || x.length == 0) {
    } else {
      // x has type String here
    } 
    
  • 🔶 Positive, negative, and run tests pass
  • 🔴 Java @NotNull annotation recognized
  • 🔴 Dotty bootstraps with explicit nulls
  • 🔴 PR to Dotty team

🔴 Feature complete

  • TBD
Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.