1.5 KiB
1.5 KiB
Patterns in AVAP
In AVAP, patterns provide a powerful way to match and destructure values. Patterns can be used in match statements to perform complex value comparisons and deconstructions. Here is a description of the available patterns and how they are used:
- Literal Patterns: Match specific literal values such as numbers, strings, or booleans. For example:
match value: case 10: # Code to execute if value is 10 case "hello": # Code to execute if value is "hello" - Variable Patterns: Capture the value of a variable. This allows you to use the matched value in the corresponding case block:
match value: case x: # Code to execute, x will be assigned the value - Sequence Patterns: Match sequences like lists or tuples. You can also use the
*operator to capture remaining elements:match value: case [1, 2, *rest]: # Code to execute, rest will capture any additional elements - Mapping Patterns: Match dictionaries or similar mappings by specifying keys and their corresponding patterns:
match value: case "key": 42: # Code to execute if the dictionary has "key" with value 42 - Class Patterns: Match instances of classes. You can also match specific attributes within the instance:
match value: case MyClass(attr1=42): # Code to execute if value is an instance of MyClass with attr1 equal to 42
Patterns in AVAP offer a flexible approach for handling different kinds of data structures and values, making it easier to write expressive and maintainable code.