▲ | simpaticoder 3 days ago | |||||||||||||||||||
Algebraic types aren't scary, but they offer enough degrees-of-freedom to become scary [1]. Software is interesting because you can move around the complexity, and putting complexity into the type system has its advocates. They like to move complexity to compile time, but there are serious trade-offs that must be acknowledged! Turing-complete type systems can get programmers in trouble. Most programs are better off putting the complexity into the body of the function being called and away from the type system itself. Use simple, fixed, runtime types[2][3], and deal with the occasional complex case in user code that you control rather than compiler code you don't. 1 - Demonstration of the Turing-completeness of Typescript's types: https://github.com/microsoft/TypeScript/issues/14833 2 - Zod https://zod.dev/ 3 - Friendly Functions https://simpatico.io/test/friendly | ||||||||||||||||||||
▲ | lock1 3 days ago | parent | next [-] | |||||||||||||||||||
No? I think you're confusing ADT with a Typescript-like type system. Adding language support for ADT is not really a warrant for an "enough DOF to become scary" sticker. I find modern Java surprisingly pretty good at capturing the essence of ADT without introducing way too much fancy stuff. Modern Java supports ADT via `sealed` classes (sum types) and `record` (product types). For working with ADT classes, modern Java provides exhaustive `switch` expression and `record` destructuring. That's enough for minimal "language ADT support". You still can't do Typescript's wacky operations like conditional / mapped types or type parameter recursion in Java. You can't do fancy Haskell's typeclasses here either. And it is certainly not turning Java's type system into a Turing-complete type system at all. | ||||||||||||||||||||
▲ | wk_end 3 days ago | parent | prev | next [-] | |||||||||||||||||||
Algebraic types, by themselves, don't do anything to make things "scary" or Turing-complete. Even C has (sort of) a weak, limited form of algebraic types through structs and unions. Zod, which you linked to as an example of the "simple, fixed, runtime types" you're advocating for supports discriminated unions too. | ||||||||||||||||||||
| ||||||||||||||||||||
▲ | WorldMaker 2 days ago | parent | prev | next [-] | |||||||||||||||||||
Zod is great, but it's a giant monster of Typescript meta-typing on your behalf. Some of it is using that "scary" Turing completeness, even, of Typescript's type system. It is nice in providing extra runtime validation that tests compiler assumptions, but it is adding complexity to do it, not removing it. Also that "Friendly Function" is about returning Sum Type. `ValidationError | int` in the example's case. It isn't saving you from a type system that understands Algebraic Types, it is absolutely relying on it (if you want compile time type safety). Also, it's only about a step or two removed from (re-)discovering the next step which is the Either<Left, Right> monad. You just need a "bind operator" to make those "Friendly Functions" easier to pipeline together and you will be off to the monad races. | ||||||||||||||||||||
▲ | whateveracct 3 days ago | parent | prev [-] | |||||||||||||||||||
ADTs don't give your type system Turing completeness. Also acting like Turing complete type systems are scary is more FUD than reality. If it becomes relevant it's because you are choosing to do recursion in some sort of constraint solver the type system provides. |