Hacker News new | ask | show | jobs
by LeafStorm 4628 days ago
I like the implementation of the "Enum" class, especially the way they allow for enums with variant behavior and "abstract Enums" (something I could have used in Java recently).

But, ever since I found out about algebraic data types from other languages, I keep wanting those. There's not quite a good way to do those in Python. (I've used both "tuple subclass" and "__slots__," but both of those have their own little quirks.)

1 comments

if python were to have algebraic data types, it might also need pattern matching.
I don't think pattern matching is necessary for ADTs. It's useful, but you could have e.g.

> if MyType.ConstructorA(a):

> ...

> elif MyType.ConstructorB(a):

> ...

or even something like

> with MyType.ConstructorA(a) as b, c:

> ...

> else with MyType.ConstructB(a) as d:

> ...

a "with/else with" construct would be a fairly straightforward addition to Python's "context manager" interface.

Yes, for better of worse Python is nowadays fairly conservative in introducing backward-incompatible syntax changes, and pattern matching would probably require quite a bit of new syntax (and benefits a lot from a strong compiler).