1.3 KiB
1.3 KiB
Literal Patterns
In AVAP, literal patterns are used to match specific literal values, such as numbers, strings, or boolean values. The syntax for a literal pattern is:
literal_pattern ::= signed_number
| strings
| "None"
| "True"
| "False"
A literal pattern only succeeds if the value of the subject is equal to the specified literal value.
Here are examples of literal patterns and their usage:
match value:
case 42:
print("Matched the number 42")
case "hello":
print("Matched the string 'hello'")
case None:
print("Matched None")
case True:
print("Matched True")
case False:
print("Matched False")
case _:
print("No match")
In this example:
case 42:matches ifvalueis exactly 42.case "hello":matches ifvalueis the string "hello".case None:matches ifvalueisNone.case True:matches ifvalueisTrue.case False:matches ifvalueisFalse.case _:is a catch-all pattern that executes if none of the previous patterns match.
Literal patterns are useful for matching specific, known values and are a fundamental part of pattern matching in AVAP.