News for hackers. Stuff that matters.
Re:Using Enumerated Types in Python by crimsonalucard1 on Saturday June 06, 2020 @04:14PM (#23441912)
imagine 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".

There are no comments on this comment yet.