patterns should be static

This commit is contained in:
Almost89 2025-02-23 20:39:12 +00:00
parent fc9a92046a
commit c64fa52b74

View file

@ -52,8 +52,7 @@ There are two main components proposed in this RFC:
The proposed grammar changes are below: The proposed grammar changes are below:
```ebnf ```ebnf
patternname = NAME | NAME '[' exp ']' | patternname '.' NAME pattern = NUMBER | STRING | 'nil' | 'true' | 'false' | stringinterp | '*' | pattern 'or' pattern | '(' pattern ')' | NUMBER 'until' NUMBER | 'not' pattern
pattern = NUMBER | STRING | 'nil' | 'true' | 'false' | stringinterp | patternname | '*' | pattern 'or' pattern | '(' pattern ')' | NUMBER 'until' NUMBER | 'not' pattern
matcharm = pattern ['if' exp] '->' exp matcharm = pattern ['if' exp] '->' exp
matcharmlist = matcharm {',' matcharm} [','] matcharmlist = matcharm {',' matcharm} [',']
matchexp = 'for' exp 'match' '(' matcharmlist ')' matchexp = 'for' exp 'match' '(' matcharmlist ')'
@ -74,22 +73,16 @@ The main purpose of a pattern is to check if a value *matches* some definition.
The *exact* pattern matches exactly what value is given to it. A pattern is an exact if given the following: The *exact* pattern matches exactly what value is given to it. A pattern is an exact if given the following:
- Strings and interpolated strings; - Strings and interpolated strings;
- Numbers; - Numbers; and finally
- Booleans; and finally - Booleans.
- Identifiers and index expressions.
Anything else produces a syntax error. Anything else produces a syntax error.
> Call expressions were considered as an addition to this list, however, they close the door to future syntax and introduce complexity with regards to multi-returns - all with little to no actual gain. > Identifiers and index expressions were included in an older version of this RFC, however, they were removed as there were few valid use cases for dynamic patterns and all they do is increase complexity.
>
> A simple solution to this problem for the user is to move the call into a variable above and use an exact pattern with the new variable.
> *Discuss in comments*: Disallowing expression-based indexing was considered with the benefit of keeping the door opening to future syntax. However, it was decided that it could be confusing that only one form of indexing is allowed and it would also prevent users from accessing specific keys which use reserved keywords.
Some examples of exact patterns would be: Some examples of exact patterns would be:
- `4` - `4`
- `"hello"` - `"hello"`
- `foo.bar`
- `true` - `true`
Exact patterns are equivalent to a binary expression checking that value `==` the exact value. This means that `4` is equivalent to `value == 4`. Exact patterns are equivalent to a binary expression checking that value `==` the exact value. This means that `4` is equivalent to `value == 4`.