3 ms·
So what to do about stuff such as lazy creation of objects? - Option 1: nullable reference and if the program crashes, you have a bug. (That should be caught d
by zvrba 4y ago
So what to do about stuff such as lazy creation of objects?
- Option 1: nullable reference and if the program crashes, you have a bug. (That should be caught during testing.)
- Option 2: Maybe<T>, making the rest of the code unreadable. Because
- Option 2a: "Force-extract" the value and get another exception if the value is not initialized
- Option 2b: Do "proper" pattern-match and do ??? if the value is not initialized.
So I strongly prefer a plain nullable reference. If I dereference it, it raises an exception that gets handled at top-level _just like any other exception due to any other kind of bug_. There's no reason that, in a properly structured program, dereferencing null should crash the whole program. (In a managed language. Unmanaged languages have their own problems - e.g., impossible to determine if a non-null pointer is at all valid. Though OS _does_ let you handle segfaults, if you want to go that way. Windows structured exceptions are much more flexible there than anything available on unix.)