Well, Rust does.
Well, Rust does.
Haskell values can throw exceptions (e.g. for division-by-zero, etc.); but catching them (and therefore branching on them) is an IO effect.
That sort of exception is treated like an unfortunate necessity in Haskell; and user code tends to use a more structured, well-typed approach like Either (there are a few alternatives floating around in common libraries)
Function std::ptr::nullCopy
pub const fn null<T>() -> *const T
> Creates a null raw pointer.Of course, the null pointer can still exist in Safe Rust, but since you can't de-reference it I think it's fine to say Safe Rust is "close enough" to count.
Citing the documentation:
> To switch to unsafe Rust, use the unsafe keyword and then start a new block that holds the unsafe code. You can take five actions in unsafe Rust, called unsafe superpowers, that you can’t in safe Rust. [...]:
> - Dereference a raw pointer
> - Call an unsafe function or method
> - Access or modify a mutable static variable
> - Implement an unsafe trait
> - Access fields of unions
> It’s important to understand that unsafe doesn’t turn off the borrow checker or disable any other of Rust’s safety checks
https://doc.rust-lang.org/book/ch19-01-unsafe-rust.html#unsa...
Rust, at least, requires that this happens in unsafe code, but for sure, a bug in unsafe code can lead to null references. We are doing noobs a disservice if we claim otherwise.
It's undefined behavior merely to create a null reference in C++, whether or not it's used. Of course that doesn't mean you can't do it anyway ("int * p = NULL; int &x = * p;") but the bug would be in the code that created the null reference, not the code that used it, just as in Rust the bug would be in the unsafe code which created the null reference—Safe Rust code is allowed to assume that this situation will never occur.
(One side effect of this rule is that the compiler is allowed to assume that "&x == NULL" will always be false for any reference "x", so even if you attempt to check for a null reference the check can be optimized out.)