santinic / pampy

Pampy: The Pattern Matching for Python you always dreamed of.
MIT License
3.52k stars 125 forks source link

OneOf operator for matching #19

Closed HarryR closed 2 years ago

HarryR commented 5 years ago

Say I want to have a match for any one of the items, instead of creating two statements to match against we only need one which specifies that the value must be one within a set.

For example:

class Choice(Enum):
    START = 0
    COOPERATE = 1
    DEFECT = 2

match(Choice.DEFECT, OneOf(Choice.DEFECT, Choice.COOPERATE), True)

This will make some types of rules more succinct, especially with support for the Enum type.

Sounds like a good idea?

santinic commented 5 years ago

Yeah, that sounds right to me :) feel free to implement it. Thanks Harry

laike9m commented 4 years ago

Feels like implementing one is easy enough, but still surprised to find it's not supported...

adamlwgriffiths commented 2 years ago

Did you get around to implementing this? Looks like the pampy code isn't pluggable - in that adding new features requires modifying the internals, not just adding new types. https://github.com/santinic/pampy/blob/master/pampy/pampy.py#L59

HarryR commented 2 years ago

No, I'd forgotten about pampy until now, the type matching in Rust is adequate.