Re:Proposing a Struct Syntax for Python
by masklinn on Wednesday June 14, 2023 @03:50PM
(#36331589)
The typed equivalent of a sum type in python is a 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.
Re:Proposing a Struct Syntax for Python
by LoganDark on Wednesday June 14, 2023 @04:32PM
(#36332191)
You'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.
Re:Proposing a Struct Syntax for Python
by masklinn on Wednesday June 14, 2023 @04:36PM
(#36332235)
Hence “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.
Then again, you can just create newtype wrappers, and that's essentially the same thing as different constructors for a sum type.