3 ms·
Isn't switch an example of refutable pattern matching in Swift? You can distinguish enums, and use where clauses: switch read(/* stuff */) { case 0:
by ridiculous_fish 11y ago
Isn't switch an example of refutable pattern matching in Swift? You can distinguish enums, and use where clauses:
switch read(/* stuff */) {
case 0:
print("EOF")
case let amt where amt > 0:
print("Read \(amt)")
default:
print("Errored")
}
The 'let amt where...' is a refutable pattern, isn't it?
- masklinn 11y agoAh yes I forgot to write the "in var/let" part which was supposed to be there. That is you can only use refutable patterns in `switch` (and `if let`/`while let` though I believe those are still restricted to Option, not general-purpose as in Rust, that correct?). In Erlang you can use a refutable match in an "assignment", that's basically a pattern assertion, a more general-purpose version of Swift's forced unwrapping.