34 ms·
There's a benefit in the cardinality during type checking when you use an external type checker. Strings have infinite possibilities an enum type is strictly de
by crimsonalucard1 6y ago
There's a benefit in the cardinality during type checking when you use an external type checker. Strings have infinite possibilities an enum type is strictly defined.
Also I'm sure with reflection you can actually implement a simple enum pattern matching function on an enum that is exhaustive and throws a runtime error immediately if every possible enum case is not covered.
- zo1 6y agoI'm not following this thread at all. What "exhaustive search" are you guys referring to? In a switch/case block? Python doesn't have that, unless I missed it while living under a rock. I.e. if you're working with python Enums, you type-check for that Enum, and then the IDE does it all for you. If you want to "check" for a specific enum, then you cast to it and the runtime Enum implementation does that checking for you by throwing an error if you try create that Enum with an invalid string value. And if you've got a raw string, before it goes too-far into your code base, you cast it into the specific Enum type, and when serializing you serialize it to the string value. Easy-peasy.
- tobywf 6y agoAFAIK, they're talking about languages like Rust, or C/C++ with `-Wswitch` (also implied by `-Werror`, which will warn you if you haven't handled all enum cases in a match/switch statement. as someone else in this thread pointed out [0], it's possible with MyPy when using if/elif/else, or if you did it with a dictionary lookup, you could write a very simple test that checks that the set of enum values is equal to the dictionary key members. I've done this before, it works well [1]. [0] https://news.ycombinator.com/item?id=23441023 https://news.ycombinator.com/item?id=23441023 [1] https://github.com/aws-cloudformation/cloudformation-cli/blob/71e17edee7193fc4bbf17940ed4d4905ca81df8a/tests/contract/test_interface.py#L21 https://github.com/aws-cloudformation/cloudformation-cli/blo...
- icegreentea2 6y agoIn some typed/compiled languages, a substantial benefit of using enums is that it provides some way to automatically determine if you've handled every eventuality. That's super not a part of normal Python as you've mentioned. The second part of crimsonalucard1's post was referring to somehow hacking in that type of functionality into Python. I think you could somehow do that with nested context managers - something like: with EnumChecker(e) as checker: with checker(enum_type.A): # do thing with checker(enum_type.B): # do thing ... Where `e` is an instance of `enum_type`, and the inner context managers acting as if/case statements, then you could on any single execution of the code block (so you only need a single test case) ensure that you covered all states.
- crimsonalucard1 6y agoimagine an enum called bool with two values: True and False. switch(value: bool): case True: // do something case False: // do something else end The above code is exhaustive in the sense that the logic covers all possible values of the enum type. The "exhaustive matching" we're talking about is similar to a pre runtime type check. So imagine where someone writes this code: switch(value): case True: // do something end The user did not handle the False case in his logic. Exhaustive pattern matching will catch this before the program runs and throw an error similar to a syntax or type check error. It will say that your switch function needs to handle all possible cases of bool. It needs to handle True AND False not just True. The power of this basically allows you to encode safety of if else logic into a type and pass that safety throughout your program. It's pretty incredible, so much so that whenever this feature is part of a language I have stopped using if else statements almost completely. Keep in mind the power of pattern matching extends beyond just exhaustive enums. An int is a form of enum as well: switch(value: int): case 0: // do something case >0: // do something else end with powerful pattern matching features the compiler should immediately let you know that your switch statement did not handle the case "<0".