You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Many good programming languages have something like match: Elm, Rust, Kotlin
match takes in a value and you specify different matching patterns; this is mainly used for enums but can also be used for matching strings and numbers.
An example with enums (in this case, the built in result type)
let wow:result[int, str] = ...let lol: int = match wow {
<ok number> -> number<err reason> -> len(reason)}
An example with strings
print(matchSystemIO.inp("Ask me a question.")! {"Who are you?" -> "I am an example.""How are you?" -> "I am good.""Why are you?" -> "Why are *you*?"
_ -> "I don't understand."})
Because match is an expression, it must return a value. Thus, a comprehensive check must be performed to ensure that every case is accounted for.
The text was updated successfully, but these errors were encountered:
because result is of type result[a, e], which is incompatible with result[b, e]. However, the type checker thinks that result could still be an ok even though that possibility was eliminated by the if let earlier.
We could make the type checker intelligent for if let and eliminate possible variants for enums somehow; however, this would be a pain because there's like three if-elses in scope.py (six in total).
Alternatively, we could add match, which would have to check that every possibility is accounted for, so we could instead do something like
let mapOk = [[a, b, e] transform: a -> b result: result[a, e]] -> result[b, e]{returnmatch result {
<ok aValue> -> ok(transform(bValue))
<err reason> -> err(reason)// `err(reason)` will be type result[*, e] which is compatible with `result[b, e]`}}
Many good programming languages have something like
match
: Elm, Rust, Kotlinmatch
takes in a value and you specify different matching patterns; this is mainly used for enums but can also be used for matching strings and numbers.An example with enums (in this case, the built in
result
type)An example with strings
Because
match
is an expression, it must return a value. Thus, a comprehensive check must be performed to ensure that every case is accounted for.The text was updated successfully, but these errors were encountered: