Live data from Hacker News

Why nullable types?

medium.com

21–30 of 119 posts

Re: Why nullable types?

#21
>There are two main solutions: Use an option or maybe type or Use a nullable type

I don't get it. These are literally all exactly the same thing, all slightly varying in ergonomics and compiler support. They may differ slightly but to call them two different categories of solution is just creating a false dichotomy for yourself.

> However, the type system is a little more flexible than with option types. The type system understands that a union type is a supertype of its branches. In other words, int is a subtype of int?. That means we can pass a definitely-present-integer to something that expects a maybe-present-integer since that’s safe to do.

You can do that in Swift, despite being placed as an example of "Solution 1".

> The short answer is that, yes, it is entirely possible to live without null, and languages like Rust do.

If you can call say that Swift has "null", then you can equally say that Rust has "null"--it's called "None". The only way "None" and "null" from other languages differ is in built-in compiler support.

Re: Why nullable types?

#23
Good article, but there is no "choice". You need both and the article even explains why!

You need union types if you have subtyping to avoid wrapping/unwrapping all the time.

And you also need option/maybe types for the cases where you must not lose information at runtime. These types can implemented by union types even, by simply "tagging" (= wrapping) the runtime values with a class).

A good language gives you both out of the box.

Re: Why nullable types?

#24
I agree that union-typed null was the best solution for Dart, but I think many of the article's criticisms of option types aren't good.

> We can’t perform arithmetic on an Option any more than we could on a List.

Why not? One sensible definition is

    Some(x) + Some(y) = Some(x + y)
    _       + _       = None()
> In fact, with Dart, we’ve found that most existing code is already statically null safe

This is a very good reason to use union types — lots of programs and lots of people are used to thinking this way, and pulling that out from under their feet wouldn't be very nice.

> Nullable types, since they have no explicit runtime representation, are implicitly flattened.

Yes, flattening things that shouldn't be flattened is the biggest problem with union types. Different people will assign different meanings to null, and it's a real headache when trying to write generic code. Discriminated unions make this a no-brainer.

> With nullable types, since there is no representation difference, you can pass a value of the underlying type directly: takesMaybeInt(3);

That's a very minor difference, but if it's that important, Rust lets you write

    fn takesMaybeInt(x: impl Into>) { ... }

Re: Why nullable types?

#25
post #9

I think dart made the wrong choice here, but I've never used the language personally or professionally. I give the author the benefit of the doubt but Optionals are so much more powerful than what this article covers. You can map, filter, reduce, chain, compose functions that all work with optionals, and lift functions that work on numbers (or any other type) to be functions that work on Optional but none of that is…

Adding an `Option` type to an existing language doesnt really solve many problems unless you can also somehow remove it's existing support for `null.

Java added `Optional` and while it makes interacting with newer API's clearer, there is nothing preventing you from passing a null Optional.

I think if you are designing a language from scratch, you should avoid null, but nullable types are a good feature for existing languages.

Having written a fair bit of kotlin, I have found the ergonomics of it's nullable type system to be really nice.

Re: Why nullable types?

#26
Me and my team prefer Option to nullable types. Option is much more clear and idiomatic FP than nullable types.

Granted, Option.map and flatMap and nullable type ?.let{} are functionally equivalent but the former just reads so much better, and we want not just functor and monad but applicative as well, no?

Eg given a List, we can use applicative to declaratively and with referential transparency etc etc (you know the usual FP sales pitch) turn it into an empty list if it contains a single non-populated option, or a list of Foo if all options in the list are populated.

With nullable types I don't think you could do it as declaratively and elegantly.

Actually, even if you could, it's kind of beside the point...

To me, the point is, Option has been designed from scratch to be an FP style Maybe type with all that comes with that in terms of it being a functor, applicative functor, monad etc etc whereas while nullable types in some ways are functionally equivalent, a nullable type doesn't implement a Functor interface, or a Monad interface, or an Applicative interface, and when it behaves like it does, it's mostly kind of by accident driven by a pragmatic need, not any real understanding of reusable, lawful FP abstractions.

Re: Why nullable types?

#27

Good article, but there is no "choice". You need both and the article even explains why! You need union types if you have subtyping to avoid wrapping/unwrapping all the time. And you also need option/maybe types for the cases where you must not lose information at runtime. These types can implemented by union types even, by simply "tagging" (= wrapping) the runtime values with a class). A good language gives you both…

> You need union types if you have subtyping to avoid wrapping/unwrapping all the time.

In my experience, the option types are always more concise. That's mostly because having option types encourages people to build out nice helper functions like these[0] and these[1]. Haskell's do notation and Rust's Try notation also save a lot of space.

[0]: https://doc.rust-lang.org/std/option/enum.Option.html

[1]: https://hackage.haskell.org/package/base-4.14.0.0/docs/Contr...

Re: Why nullable types?

#28
Nullable type is the biggest mistake in all the "modern" languages

It should be a type Optional, not built into a language, it make no sense memory wise

It's like NULL, it should stay, it is not an issue, it is how computers work!!!!!!!

Re: Why nullable types?

#29

>There are two main solutions: Use an option or maybe type or Use a nullable type I don't get it. These are literally all exactly the same thing, all slightly varying in ergonomics and compiler support. They may differ slightly but to call them two different categories of solution is just creating a false dichotomy for yourself. > However, the type system is a little more flexible than with option types. The type sys…

They're not the same, and the biggest difference is explained in the Map> example: union types merge different uses of null that you usually don't want merged.

Re: Why nullable types?

#30

>There are two main solutions: Use an option or maybe type or Use a nullable type I don't get it. These are literally all exactly the same thing, all slightly varying in ergonomics and compiler support. They may differ slightly but to call them two different categories of solution is just creating a false dichotomy for yourself. > However, the type system is a little more flexible than with option types. The type sys…

This comment is misleading. They may appear to be the same but they are not because in certain languages (like Rust) you are forced to handle the `Option` case when a value is `None` which guides a programmer's thinking in the direction of what to do in that situation.

Without these higher level types runtime null pointer exceptions are very common, using `Option` or `Maybe` creates a situation where these sorts of errors are a lot less likely.

Post reply on HN