Fate of null in Scala

Have we considered that compiler should give a warning in these scenarios?

var ctx:Context = null

case class Employee(val age:Int, val reports:List[Employee] = null)

def readFile(path:String):String = {
  if(!File.exists(path)) {
    return null

Basically whenever we are setting or returning null. The only risk I see is that some projects might have setting to fail if warning present, but am sure it would be helpful to them.

1 Like

Maybe wartremover has something for this?

(And/or perhaps there are scalafix rules out there?)


There’s Explicit Nulls in Scala 3. It’s still experimental, and unless I’ve been doing something wrong, it doesn’t seem to consistently honor the corresponding annotations in Java libs, so I had to #nn the hell out of Java interop in my experiments.

1 Like