From 6a83c9e72114eb9a2430fc9a99385ce67cd92217 Mon Sep 17 00:00:00 2001 From: james Date: Tue, 2 Jan 2024 10:12:46 +1100 Subject: [PATCH] #33: Update documentation --- docs/_posts/2023-12-21-switch-expressions.md | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/docs/_posts/2023-12-21-switch-expressions.md b/docs/_posts/2023-12-21-switch-expressions.md index de63ae7..1f953bf 100644 --- a/docs/_posts/2023-12-21-switch-expressions.md +++ b/docs/_posts/2023-12-21-switch-expressions.md @@ -395,7 +395,8 @@ switch (x) { If you want to have a pattern that depends on the value of an existing variable then you can use `$` to expand the value of that variable inside the pattern. -For example, to match any list whose first element has the same value as the variable `v`: +For example, to match any list whose first element has the same value as the variable `v` or to +match a two element list whose first element is twice the value of `v` and whose last element is `v`: ```groovy switch (x) { [$v,*] -> 'matched' @@ -433,15 +434,20 @@ This is to allow you to be able to match on exact types, especially in pattern m For example, consider this: ```groovy switch (x) { - 1 -> 'int 1' - 1L -> 'long 1' - 1D -> 'double 1' - 1.0 -> `Decimal 1` + 1 -> 'int 1' + 1L -> 'long 1' + 1D -> 'double 1' + (Decimal)1 -> 'Decimal 1' + 1.0 -> 'Decimal 1.0' } ``` When `x` is `1` it will match the pattern that exactly matches its type and value so a `long` will only match `1L`, for example. +Note that `(Decimal)1` is different to `1.0`. +Even though both are of type `Decimal`, `1` is treated differently to `1.0` because the number of decimal +places is significant. + This means that this will return `false` since the `long` value will not match against an `int` value of `1`: ```groovy