Alternative patterns
Alternative patterns can be given for a case clause using the
|
operator. If any of the patterns match then the clause matches.
case 句には |
演算子を使って別のパターンを指定することができます。いずれかのパターンがマッチすれば、その節はマッチします。
If a pattern defines a variable then all of the alternative patterns for that clause must also define a variable with the same name and same type.
あるパターンが変数を定義している場合、その節のすべての代替パターンも同じ名前で同じ型の変数を定義している必要があります。
Currently it is not possible to have nested alternative patterns, so the
pattern [1 | 2 | 3]
is not valid.
現在のところ、代替パターンを入れ子にすることはできないので、パターン [1 | 2 | 3]
は無効です。
import gleam/io
import gleam/int
pub fn main() {
let number = int.random(10)
io.debug(number)
let result = case number {
2 | 4 | 6 | 8 -> "This is an even number"
1 | 3 | 5 | 7 -> "This is an odd number"
_ -> "I'm not sure"
}
io.debug(result)
}