3 ms·
While I initially thought this was absurd, looking at the intended use case I think this is actually sensible behavior. While I would probably use a word other
by jjk166 6y ago
While I initially thought this was absurd, looking at the intended use case I think this is actually sensible behavior. While I would probably use a word other than case to further distinguish it from the switch statements of other languages, the fact remains it's not a switch statement, and assigning values to variables is the whole point.
In the article's example, copied below, the programmer is confident that the status code fits the format they are looking for but not what status code corresponds to not found
NOT_FOUND = 404
match status_code:
case 200:
print("OK!")
case NOT_FOUND:
print("HTTP Not Found")
This should be handled by an elif statment. If on the other hand we're dealing with
match ambiguous_response:
case status_code:
print("Responded with status code: ",status_code)
case { "status_code" : status_code , _ }:
print("Responded with json obj containing status code: ",status_code)
doing so with elif statements would be cumbersome at best.
- utucuro 6y agoPeople seem to read the words "Structural pattern matching" and interpret it as "Pattern matching"... It's a neat little misnamed feature, that's all...