Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Sum 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-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.



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: