assistance-engine/docs/developer.avapframework.com/chapter9_48.md

30 lines
1.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

## Group Patterns
In AVAP, group patterns are used to group multiple patterns together. The syntax for a group pattern is:
```javascript
group_pattern ::= "(" pattern ")"
```
Group patterns are useful when you want to combine patterns or when patterns need to be evaluated together. They have the same effect as the pattern they contain but allow for more complex pattern structures.
Heres an example of how to use group patterns in AVAP:
```javascript
match value:
case (42 | 43):
print("Matched either 42 or 43")
case (name, age) if age {'>'} 18:
print(f"{name} is an adult")
case _:
print("Matched something else")
```
In this example:
* `case (42 | 43):` uses a group pattern to match either the value 42 or 43.
* `case (name, age) if age {'>'} 18:` uses a group pattern to match a tuple and includes an additional condition on the age.
* `case _:` matches any other value not covered by the previous cases.
Group patterns are ideal for creating more complex matching scenarios where patterns need to be combined or grouped together.