43 lines
1.3 KiB
Markdown
43 lines
1.3 KiB
Markdown
## 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:
|
|
|
|
```javascript
|
|
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:
|
|
|
|
```javascript
|
|
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 if `value` is exactly 42.
|
|
* `case "hello":` matches if `value` is the string "hello".
|
|
* `case None:` matches if `value` is `None` .
|
|
* `case True:` matches if `value` is `True` .
|
|
* `case False:` matches if `value` is `False` .
|
|
* `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.
|