3 ms·
The typed equivalent of a sum type in python is a union (https://docs.python.org/3/library/typing.html#typing.Union https://docs.python.org/3/library/typing.htm
by masklinn 3y ago
The typed equivalent of a sum type in python is a union (https://docs.python.org/3/library/typing.html#typing.Union https://docs.python.org/3/library/typing.html#typing.Union). Works with pretty much every type you can think of, but has the usual union limitation that you can't have two variants of the same type (e.g. int | int | float).
Then again, you can just create newtype wrappers, and that's essentially the same thing as different constructors for a sum type.
- LoganDark 3y agoYou'd need to create newtype wrappers anyway in order to tell them apart, I'd assume. Otherwise it wouldn't really be an algebraic data type as much as a... union type.
- masklinn 3y agoHence “equivalent”. Of your variants all have trivial and non-overlapping payloads you can just use them as-is, or erlang-style tagged tuples, in most cases it’ll do fine.