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

1.1 KiB
Raw Permalink Blame History

Group Patterns

In AVAP, group patterns are used to group multiple patterns together. The syntax for a group pattern is:

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:

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.