Skip to content

Commit

Permalink
#33: Update documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
jaccomoc committed Jan 1, 2024
1 parent 8022d12 commit 6a83c9e
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions docs/_posts/2023-12-21-switch-expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6a83c9e

Please sign in to comment.