Skip to content

Commit

Permalink
More edits
Browse files Browse the repository at this point in the history
  • Loading branch information
jariji authored and thautwarm committed Feb 19, 2024
1 parent a522a0a commit 9c9eb00
Showing 1 changed file with 34 additions and 8 deletions.
42 changes: 34 additions & 8 deletions docs/syntax/pattern.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,45 @@ julia> @match 10 begin
"right!"
```

All literal data introduced with Julia syntax can be matched by literal patterns. However, exact `===` equality is required only for primitive types(`Int8-64`, `UInt8-64`, `Bool`, etc.) and singleton types(`struct Data end; Data()`); other types may match more flexibly. For example, substrings can match a literal string pattern, and a single integer value can match a range pattern like `1:5` that contains it.
You can think of this match expression like

```julia
VALUE = 10
if VALUE === 1
"wrong!"
elseif VALUE === 2
"wrong!"
elseif VALUE === 10
"right!"
else
error("matching non-exhaustive")
end
```

All literal data introduced with Julia syntax can be matched by literal patterns. However, matching with exact `===` equality is required only for primitive types (`Int8-64`, `UInt8-64`, `Bool`, etc.) and singleton types (e.g. `struct Data end; Data()`). Other types may match more flexibly: for example, a `SubString` can match a literal string pattern; a single integer value like `3` can match a range pattern like `1:5` that contains it.

Capturing Patterns
--------------------------

A pattern where there is a symbol such as `x` on the left hand side will bind the input value to that symbol and let you use that captured value on the right hand side
A pattern where there is a symbol such as `x` on the left hand side will bind the input value to that symbol and let you use that captured value on the right hand side:

```julia-console
julia> @match 1 begin
x => x + 1
end
2
```

This match expression is like

```julia
VALUE = 1
let x = VALUE
x + 1
end
```


You can put `_` on the left hand side of a pattern if you don't care about what the captured value is.

However, sometimes a symbol might not be used for capturing. If and only if some visible global variable `x` satisfying `MLStyle.is_enum(x) == true`, `x` is used as an enum pattern.
Expand All @@ -66,7 +92,7 @@ julia> @match 1 begin
1
```

Guards
`if` patterns
--------------------

Writing `if cond end` as a pattern will match if `cond==true`
Expand All @@ -84,8 +110,8 @@ MLStyle.jl allows you to put guards anywhere during matching.

Sometimes, in practice, you might want to introduce type variables into the scope, in this case use `where` clause. See [Advanced Type Patterns](#advanced-type-patterns) for more details.

Predicates
---------------
`GuardBy` patterns
-------------------

Equivalent to guard patterns, writing `GuardBy(f)` in a pattern will match if and only if `f` applied to the pattern matching input gives true:

Expand All @@ -95,18 +121,18 @@ function pred(x)
end

@match x begin
GuardBy(pred) => 5 - x # only succeed when x > 5
GuardBy(pred) => 5 - x # only succeeds when x > 5
_ => 1
end

@match x begin
GuardBy(x -> x > 5) => 5 - x # only succeed when x > 5
GuardBy(x -> x > 5) => 5 - x # only succeeds when x > 5
_ => 1
end
```


And-Patterns
And-Patterns
--------------------

`pat2 && pat2` on the left hand side of a pattern will match if and only if `pat1` and `pat2` match individually. This lets you combine two separate patterns together,
Expand Down

0 comments on commit 9c9eb00

Please sign in to comment.