5 ms·
In my view the next nice thing to have is algebraic data types with compile-type exhaustiveness checks. I believe several languages implement null safety using
by baddox 6y ago
In my view the next nice thing to have is algebraic data types with compile-type exhaustiveness checks. I believe several languages implement null safety using a simple algebraic data type that can be “Nothing” or “Some(‘a)” where “a” is a type. The compiler can enforce that you can never access the “a” without checking for both cases: “Nothing” and “Some(‘a)”.
That extends naturally to any number of types. For example, a type representing a network request can be “Loading” or “Loaded(‘data)” or “Error(‘error)” and you can’t just blindly access the data without also handling loading states and error states.
I absolutely love this when doing frontend work in ReasonML, and I miss it in JavaScript. TypeScript definitely has this functionality but I’ve never felt that it was natural to use.
- blandflakes 6y agoI'd agree; if I had to guess what's most likely to become mainstream soon, it would be union types with matching (like Either or Result types). Strange that Go did the lazy thing and made the type signature a tuple, requiring you to match yourself...
- saagarjha 6y agoGo explicitly eschews more advanced features in its type system–it not being "lazy", it thinks that such things are too complicated to be in the language. Most other up-and-coming languages have chosen to include proper sum types, including Swift and Rust.
- mumblemumble 6y ago> it thinks that such things are too complicated to be in the language. Yep. Though, there are two ways to interpret that statement. One of the things that has always slightly annoyed me about Go's messaging is that it conflates "complicated for the language's implementers" and "complicated for the language's users." To the point where it sometimes verges on being a sort of mild gaslighting.
- saagarjha 6y agoI personally find languages that treat their users as stupid (providing facilities to the language designers but not its users) to be infuriating. Go is pretty much the paragon of this philosophy.
- mumblemumble 6y agoI'm not even sure that the intent is to treat users as stupid. It seems just as likely that it's just a collective experiment in elevating sour grapes into an art form.
- blandflakes 6y agoYour more charitable characterization is certainly reasonable, but I find that pushing the complexity of matching the cases to the users (including the IMO invalid state of "returned a value AND an error") just results in a below-MVP error system.
- satvikpendem 6y agoDart has compile time ADTs with the freezed package. I like Dart since it has code generation features so you can write or import custom language features, much like JS with Babel. https://pub.dev/packages/freezed https://pub.dev/packages/freezed