17 ms·
In those other languages, if `not_found` is equal to 404 before the match block, how do you write a clause that only matches if `status` is 404? Do you have to
by pansa2 6y ago
In those other languages, if `not_found` is equal to 404 before the match block, how do you write a clause that only matches if `status` is 404?
Do you have to use the integer literal 404 instead of the named integer `not_found`?
- yw3410 6y agoDepends on the language, but a lot of them use guard clauses (so case ... if cond =>).
- DougBTX 6y agoPretty much the same as in the PEP: https://www.python.org/dev/peps/pep-0636/#adding-conditions-to-patterns https://www.python.org/dev/peps/pep-0636/#adding-conditions-...
- deleted 6y ago[deleted]
- heisenzombie 6y agoElixir uses the “pin operator” “Use the pin operator ^ when you want to pattern match against a variable’s existing value rather than rebinding the variable.” iex> not_found = 404 404 iex> ^not_found = 200 ** (MatchError) no match of right hand side value: 200 https://elixir-lang.org/getting-started/pattern-matching.html#the-pin-operator https://elixir-lang.org/getting-started/pattern-matching.htm...
- pfalcon 6y agoExactly, Elixir is usually the only example given. So, how it makes sense to blame Python here is unclear. There's a proposal to add similar "sigils" to Python pattern matching, it just didn't make it yet.
- lou1306 6y agoIn OCaml and F# you can use `when`: match status with | n when n = not_found -> ...