mirror of
https://github.com/roc-lang/roc.git
synced 2025-12-15 21:23:57 +00:00
Revert "Add a section about constants in patterns"
This reverts commit ed27885391b4ed686dc615872fbff77f8122a358.
This commit is contained in:
parent
77d9cef014
commit
9ecc304d25
1 changed files with 6 additions and 31 deletions
|
|
@ -377,43 +377,18 @@ when color is
|
|||
Green | Red | Yellow -> 4 # only gets run if totalColors < 3
|
||||
```
|
||||
|
||||
This gives you a way to use local variables in patterns:
|
||||
This gives you a way to use constants in patterns:
|
||||
|
||||
```rust
|
||||
\min, max ->
|
||||
when number is
|
||||
0 -> "zero"
|
||||
1 -> "one"
|
||||
v if v == min -> "min"
|
||||
v if v == max -> "max"
|
||||
_ -> "other"
|
||||
```
|
||||
|
||||
You can also use constants in pattern matches by fully-qualifying them:
|
||||
|
||||
```rust
|
||||
when number is
|
||||
0 -> "zero"
|
||||
1 -> "one"
|
||||
Num.pi -> "pi"
|
||||
_ -> "other"
|
||||
```
|
||||
|
||||
In Roc, you can also write `Foo.bar` inside the `Foo` module itself (and it even works for
|
||||
unexposed things in the current module), which gives you a way to refer to top-level constants
|
||||
in the same module.
|
||||
|
||||
So so the following code would work as expected, assuming it's written at the top level of
|
||||
a module named `Foo`:
|
||||
|
||||
```rust
|
||||
constant = 42
|
||||
pi = 3.14
|
||||
e = 2.72
|
||||
|
||||
when number is
|
||||
0 -> "zero"
|
||||
1 -> "one"
|
||||
Foo.constant -> "constant"
|
||||
_ -> "other"
|
||||
v if v == pi -> "pi"
|
||||
v if v == e -> "e"
|
||||
_ -> ""
|
||||
```
|
||||
|
||||
## Custom Types
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue