From c1816abb13fb0de048f086cc780db0bbab071e8e Mon Sep 17 00:00:00 2001 From: nucklass <60252281+nucklass@users.noreply.github.com> Date: Mon, 17 May 2021 19:33:36 -0700 Subject: [PATCH 1/3] Add Example to Reference Pattern Docs --- docs/syntax/pattern.md | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/docs/syntax/pattern.md b/docs/syntax/pattern.md index 66a66d3..4d7d0bf 100644 --- a/docs/syntax/pattern.md +++ b/docs/syntax/pattern.md @@ -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 = ... @@ -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" +``` +Internally, literal pattern matching behaves with strict equality, 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 From 746682ae7f6ba5b1d1ad35482617e49862f39ec4 Mon Sep 17 00:00:00 2001 From: nucklass <60252281+nucklass@users.noreply.github.com> Date: Wed, 19 May 2021 13:07:20 -0700 Subject: [PATCH 2/3] clarify === for reference patters in docs --- docs/syntax/pattern.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/syntax/pattern.md b/docs/syntax/pattern.md index 4d7d0bf..5576f29 100644 --- a/docs/syntax/pattern.md +++ b/docs/syntax/pattern.md @@ -271,7 +271,7 @@ end # => "there is not a match" _ => "there is not a match" end # => "there is a match" ``` -Internally, literal pattern matching behaves with strict equality, 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. +When matching a primitive type or an immutable, size-zero type literal pattern matching behaves with strict equality, 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 From fc92a74f80ec825cf9c0b93cd6f3751d053a4b29 Mon Sep 17 00:00:00 2001 From: nucklass <60252281+nucklass@users.noreply.github.com> Date: Wed, 19 May 2021 13:11:11 -0700 Subject: [PATCH 3/3] clarify === for reference patters in docs, fix grammar --- docs/syntax/pattern.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/syntax/pattern.md b/docs/syntax/pattern.md index 5576f29..48b8fc7 100644 --- a/docs/syntax/pattern.md +++ b/docs/syntax/pattern.md @@ -271,7 +271,7 @@ end # => "there is not 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, 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. +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