I’ve been playing with the explicit null support in Scala 3. I’m working on updates to my textbook and I’m hoping to include that as a default setting for all of my examples. I’m basing this on the belief that this is the direction the language will move in the long run. For most of my examples, it doesn’t matter. Where it does come up though is when dealing with Java libraries and one in particular,
I often have students do simple parsing of strings with
split, but the return type of
split with explicit nulls turned on is currently
Array[String | Null] | Null, which requires a lot of rather verbose handling to deal with. This makes it pretty much unusable for introductory students. In theory, I could have them turn off the explicit nulls for the sections of code that do this, but that feels like the wrong approach.
I’m not certain what the right approach is to get around this. Ideally, methods in
java.lang.String (and possibly other classes in
java.lang) that can’t return
nulls would have a Scala signature that doesn’t include them. Almost as good would be extension methods with slightly different names that have appropriate types. This approach can’t be done for everything, but given that string literals in Scala use these methods, this is probably worth a special case.
This is something I’d be interested in helping with if I knew what approach would be acceptable.