3 ms·
Aren't null pointers just a variation on the Maybe Monad?
by user2994cb 9y ago
Aren't null pointers just a variation on the Maybe Monad?
- anaphylactic 9y agoMaybe is just another Optional type, but with a much weirder name, so that's pretty obvious. But this makes me wonder - does Haskell optimize `Maybe`-like types down to a null-pointer, like Rust does?
- user2994cb 9y agoDunno, but I'd have thought that you can make None look like a null pointer, but Some(p) needs to be tagged in some way.
- user2994cb 9y agoActually, having Some(x) be just a pointer to x seems to work fine without an explicit tag, as dragonwriter says below.
- simon_o 9y agoNo, because Option/Maybe can nested, nulls can't.
- dragonwriter 9y agoNulls can be nested if you are using a language with explicit pointer types. E.g., Int*** x; Can be a lot like Maybe Maybe Maybe Int It's just that in languages where a type like Integer really means “an integer or a null", the nullability doesn't nest, and no one wants to deal with naked pointers.
- Zpalmtree 9y agoYou're forced to check for a Nothing value, wheras with a null pointer you can happily use it like a normal value.
- masklinn 9y agonull pointers can be encoded as an option type[0] (that's basically what languages like Swift or Rust do, they eschew nullable pointers and wrap non-nullable pointers in an option type instead), but they are not because 1. you don't have the attending type-safety of knowing that some pointers can't be null 2. the compiler requiring checking for nullity before every pointer access would be horrendously unwieldy [0] which can then be compiled back to a regular nullable pointer at runtime