Re:Is your programming language unreasonable? (2015)
by masklinn on Saturday October 10, 2020 @08:26AM
(#24738486)
> So this essentially means there can never be a NULL object assigned to anything…
No, it means that you can't leave bits off yet claim to have a valid object, either the object is fully initialised or it's illegal. This is mostly a concern in older languages like C where you can reserve space for a struct then not fill in anything.
In some more recent languages (Java, Go, C# historically though that's changing) everything is zeroed by default so you avoid the "random garbage" situation, but it means you need to assume anything can be zero even if that's not intentional, and depending on the specifics you might "double write" each location even when that's not actually necessary.
In the more functional slate of languages, you simply can't let any members off. There might be a concept of "default value" but that tends to be opt-in, the baseline is that if you define a type you must fill in every member, explicitly, before the compiler will accept it.
Initialising items to null is orthogonal.
Now since the rest is predicated on a misunderstanding I've a version of each for "incomplete initialisation" and a version for "null".
> how is [forcing complete initialisation] possible?
The compiler just requires that every member of the structure be specified.
> how is [things never being NULL] possible?
Don't make NULL an implicit member of every type.
> is [incomplete initialisation] avoided by throwing exceptions instead of returning NULL?
it's avoided by the compiler refusing to compile your code if you leave off members.
> is [things never being null] avoided by throwing exceptions instead of returning NULL?
It's avoided by NULL being a member of a completely different type than anything else, whether that type is a special case or not e.g. in Rust "null" is one member of the Option "wrapper type" so to indicate that, say, a user is optional you write `Option<User>`, then that can either be `None` (no user, ~ null) or `Some(user)`.
If you just type something as `User` then it's necessarily a valid user and nothing else, because there is no null value you could put into it, you'd get a type error.
No, it means that you can't leave bits off yet claim to have a valid object, either the object is fully initialised or it's illegal. This is mostly a concern in older languages like C where you can reserve space for a struct then not fill in anything.
In some more recent languages (Java, Go, C# historically though that's changing) everything is zeroed by default so you avoid the "random garbage" situation, but it means you need to assume anything can be zero even if that's not intentional, and depending on the specifics you might "double write" each location even when that's not actually necessary.
In the more functional slate of languages, you simply can't let any members off. There might be a concept of "default value" but that tends to be opt-in, the baseline is that if you define a type you must fill in every member, explicitly, before the compiler will accept it.
Initialising items to null is orthogonal.
Now since the rest is predicated on a misunderstanding I've a version of each for "incomplete initialisation" and a version for "null".
> how is [forcing complete initialisation] possible?
The compiler just requires that every member of the structure be specified.
> how is [things never being NULL] possible?
Don't make NULL an implicit member of every type.
> is [incomplete initialisation] avoided by throwing exceptions instead of returning NULL?
it's avoided by the compiler refusing to compile your code if you leave off members.
> is [things never being null] avoided by throwing exceptions instead of returning NULL?
It's avoided by NULL being a member of a completely different type than anything else, whether that type is a special case or not e.g. in Rust "null" is one member of the Option "wrapper type" so to indicate that, say, a user is optional you write `Option<User>`, then that can either be `None` (no user, ~ null) or `Some(user)`.
If you just type something as `User` then it's necessarily a valid user and nothing else, because there is no null value you could put into it, you'd get a type error.
* Rust playground which fails to compile because one of the struct members was left off: https://play.rust-lang.org/?version=stable&mode=debug&editio...
* Rust playground which fails to compile because trying to put a null (None) in a non-nullable field: https://play.rust-lang.org/?version=stable&mode=debug&editio...
* Version of the previous with an optional (nullable) member: https://play.rust-lang.org/?version=stable&mode=debug&editio...
* Version with opt-in default (through Rust's Default trait, the default derivation of the Default trait will recursively default each field, which usually means some sort of zeroing): https://play.rust-lang.org/?version=stable&mode=debug&editio...