Hacker News new | past | comments | ask | show | jobs | submit login

Aren't null pointers just a variation on the Maybe Monad?



Maybe 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?


Dunno, 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.


Actually, having Some(x) be just a pointer to x seems to work fine without an explicit tag, as dragonwriter says below.


null 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


You're forced to check for a Nothing value, wheras with a null pointer you can happily use it like a normal value.


No, because Option/Maybe can nested, nulls can't.


Nulls 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.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: