3 ms·
The correct way to handle this in Scala is to wrap the possibly-null reference (which I assume you're getting as a return value from some Java library) in Optio
by aianus 10y ago
The correct way to handle this in Scala is to wrap the possibly-null reference (which I assume you're getting as a return value from some Java library) in Option(), not manually checking for null.
- philipkglass 10y agoBy "manually checking" I mean that I have to use my own brainpower to notice places where null can leak in to code I'm writing; the compiler doesn't actually enforce the type safety I thought I had. I do use Option to handle sometimes-nulls after I notice them. EDIT: since Jython doesn't allow raw nulls to leak in from Java libraries, and Kotlin prevents accidental use of nullable values at compile time, it naively seems like Scala could prevent accidental null introduction despite living on the JVM. Or as a distant second-best, prominently warn about the perils of null whenever introducing its type system to beginners.
- yawaramin 10y agoAren't Jython `None` values essentially `null`s? But, Kotlin, Swift, and others are starting to get it right. Scala will follow suit--the plan is for 'Dotty', the next major version of the Scala compiler, to represent nullable types explicitly as `A | Null` (`A` or `Null`). Without that explicit type there will be no way for a type to hold `null`. Until that happens, in Scala we continue to work through in the presence of potential nulls by wrapping stuff that comes from Java APIs in `Option`s and never explicitly returning `null` from any Scala APIs.