4 ms·
> Sum types are relatively rare in modern programming languages, outside functional programming and some places like Rust. Is that true? I guess it depends on
by ks2048 1y ago
> Sum types are relatively rare in modern programming languages, outside functional programming and some places like Rust.
Is that true? I guess it depends on what "modern" means. But, for popular languages less that ~20 years old seem to all have them AFAIK, except Go, I think.
Swift being one I've used which I like a lot (Swift enum).
- armchairhacker 1y agoSum types are effectively implemented in Java, Scala, and Kotlin, via sealed classes and (Java “record”, Scala “case class”, Kotlin “data class”)es; and in TypeScript via discriminated unions (https://www.typescriptlang.org/docs/handbook/unions-and-intersections.html#discriminating-unions https://www.typescriptlang.org/docs/handbook/unions-and-inte...). By “effectively” I mean you can construct and pattern-match these exactly like sum types, with compile-time exhaustiveness checking. Sure, you can do extra things with them (like share one variant between multiple types or compare instances by pointer), so some may argue they aren’t “real” sum types. But there’s nothing you can do with “real” sum types that you can’t with them, so I think they’re just a superset.