🔤 Language + Union Type = ❤️
A feature that every programming language needs
#go #rust #ts #haskell
# [ $davids.sh ] · message #249
🔤 Language + Union Type = ❤️
A feature that every programming language needs
#go #rust #ts #haskell
@ [ $davids.sh ] · # 1538
I love Union Types because they provide an incredibly convenient way to get rid of interfaces, as well as OOP Inheritance and Polymorphism, thanks to the advent of Pattern Matching:
type UnactivatedUser = { email: string; password: null; activated: false; }
type ActivatedUser = { email: string; password: string; activated: true; }
type User = UnactivatedUser | ActivatedUser // this is a union type
function someFn(user: User) { switch(typeof user) { // this is Pattern Matching case UnactivatedUser: throw new Error("User must be activated") case ActivatedUser: return true default: safeGuard(user) // if we add another type to User, then here at compile time there will be an error that we haven't added another case for it } }
With some modifications, this pseudocode can be implemented in TS, Rust, and Haskell.
But Go doesn't have this... and that's why you have to use interfaces in a huge number of places and come up with some kind of universal logic, instead of checking the type on the spot and doing what you need.
Union Types and Pattern Matching are such useful techniques that I made them part of FOP, as they also provide access to Algebraic Data Types.
– https://fop.davidshekunts.ru/#d04ae9b9e8e24f3ab46144f1e8816fc2 – https://itnext.io/practical-introduction-to-algebraic-datatypes-adts-in-typescript-1cb6952e4c6d – https://wiki.haskell.org/OOP_vs_type_classes – https://doc.rust-lang.org/book/ch18-03-pattern-syntax.html
@ Vassiliy ITK Kuzenkov · # 1542
This is one of those things that are terribly lacking in other languages, along with the solution to the expression problem via traits in Rust/protocols in Clojure.