lampepfl / dotty-feature-requests

Historical feature requests. Please create new feature requests at https://github.com/lampepfl/dotty/discussions/new?category=feature-requests
31 stars 2 forks source link

Add ? as a postfix type operator to denote nullable type #114

Open lilac opened 4 years ago

lilac commented 4 years ago

Since we have union types and explicit null now, it is easy to represent nullable type in Java as T | Null, but that is verbose compared to peers in Kotlin and Swift.

The union types and intersection types in Dotty is more powerful than competitors, but from users' point of view, typing T | Null is worse than T? in other languages.

Therefore, I propose to add a postfix type operator ?, just like | and & in union types and intersection types. In other words, T? = T | Null.

odersky commented 4 years ago

That would make sense if T | Null was very common in Scala. But is it? I have the impression null is rarely used, outside of Java interop. Put in other words: Why spend shorthand syntax on null, but not Option?