A full-blown union type is indeed one way to solve it, but adding union types to Java would not solve it, since Java’s String type already allows null. First step is to fix this mistake in type-system. So, more type-safe languages start with not allowing null for String type and then add some mechanism to deal with the case of “value missing” (union type is just one way to deal with it).

Written by

Project Lead for the Kotlin Programming Language @JetBrains

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store