Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Example to Reference Pattern Docs #122

Merged
merged 3 commits into from
May 20, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion docs/syntax/pattern.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ julia> @match 1 begin
Reference Patterns
-----------------

This feature is from `Elixir` which could slightly extends ML based pattern matching.
This feature is known as the `pin operator` from `Elixir` which could slightly extend ML based pattern matching.

```julia
c = ...
Expand All @@ -256,6 +256,22 @@ c = ...
_ => "none of x and y equal to c"
end
```
Reference Patterns are useful, for example, when it's necessary to match on the values of numeric variables, but not the type:

```julia
c = Int16(10) # c is of type Int16

@match c begin
10.0 => "there is a match" #pattern is a Float
_ => "there is not a match"
end # => "there is not a match"

@match c begin
&10.0 => "there is a match"
_ => "there is not a match"
end # => "there is a match"
```
When matching a primitive type or an immutable, size-zero type literal pattern matching behaves with strict equality. This behavior is similar to the [`===`](https://docs.julialang.org/en/v1/base/base/#Core.:===) operator in base Julia. Reference patterns behave more like the [`==`](https://docs.julialang.org/en/v1/base/math/#Base.:==) operator in base Julia, where the type of the numeric variable is ignored, and only abstract values are compared.


Macro Call Patterns
Expand Down