| https://old.lispcast.com/what-are-product-and-sum-types/ That was easy to understand. > You can have 100% type-safe, guaranteed at compile time code without null that can still represent the absence of data. If it's a single score, I'd still want to use null / int. There no invalid states being represented, anything else is still unnecessary complexity. > Nullness infects your data model and always comes out of nowhere in production and ruins your day. A TS example: trying to do anything with 'score' where score is `null | number`, would be caught be the compiler. |
In your example, you still have to manually check if there's a value every time, but this is not compiler-enforced. Should you forget, you will get a runtime crash at some point (likely in production at a critical time) with some kind of arithmetic error. This wouldn't be possible with a simple sum type.
Also, a sum type with units of Score(Int) and NoScore won't allow assignments of any other "null" instances. Null in one spot is interchangeable with null in any other spot, and this can lead to bugs and runtime crashes. Null should be avoided when possible.