-
Notifications
You must be signed in to change notification settings - Fork 66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Roundup of proposed patterns on vblang #367
Comments
And the formal grammar of the second set of patterns: Pattern
// patterns with subpatterns
: Pattern ',' Pattern // OR pattern
| '(' Pattern (',' Pattern)* ')' // tuple pattern
| 'Not' Pattern // NOT pattern
| 'With {' MemberPattern (, MemberPattern)* '}' // with pattern
// patterns without subpatterns
| 'As' TypeName // typecheck pattern
| 'Dim' Identifier ('As' TypeName)? // variable + typecheck pattern
| 'Is'? ComparisonOperator Expression // comparison pattern
| 'Like' StringExpression // Like pattern
| Expression 'To' Expression // range pattern
| Expression // expression pattern
;
MemberPattern
: '.' Identifer Equals Expression
| '.' Identifier `Matches` Pattern
; |
How would the
mean? Would it match only if the tested expression is not a tuple of |
No |
Currently Also, I think the AND pattern has less utility, as most of the patterns cannot be combined together. For example, trying to match something which is both a tuple (tuple pattern) and has a public |
We could just disallow use of |
Maybe we can use this syntax:
|
Except that the current Or pattern doesn't require any keyword. It's probably a no-brainer that backwards compatibility must be preserved. |
We can treat it as a special case when used at the top level. |
As a follow-up to #337 (comment), this is a collection of proposed patterns described in the various issues related to pattern matching (#124 and #337).
@AnthonyDGreen's patterns:
*
Nothing
?
] [As
typename]The variable's type is that of the value to be matched, if not specified in the pattern
Without
?
, match fails onNothing
With
?
, match succeeds onNothing
; the variable's type is nullable(
[ pattern1 [,
pattern2 ...] ])
Sub-patterns can be used in place of
ByRef
parameters, whose value must match the corresponding patternAllows extending pattern matching (AKA F# active patterns)
(
pattern1 [,
pattern2 ...])
{
pattern1 [,
pattern2 ...]}
Interpolations are treated as sub-patterns which the variable strings are matched against
XElement
if theXElement
can be deconstructed based on the constant partsEmbedded expressions are treated as sub-patterns, which are matched against corresponding content
Note: This set of patterns assumes the use of
Matches
(or some other dedicated keyword) inCase
clauses.@bandleader / @zspitz 's patterns:
Is
] comparison-operator expressionLike
string-expressionLike
the specified string expression1To
expressionDim
identifier [As
typename]The variable's type is that of the value to be matched, if not specified in the pattern
A value of
Nothing
matches if the variable's type can holdNothing
2As
typenameA value of
Nothing
matches if typename can holdNothing
2,
pattern(
pattern [,
pattern ...])
Not
patternWith {.
identifier=
expression ...}
With {.
identifierMatches
pattern ...}
Multiple properties/fields can be tested against
Footnotes:
Case
pattern, without a dedicated keyword. Therefore, compatibility with existingCase
syntax needs to be preserved.Nothing
from reference typesCase
expression, it's important that all patterns not be valid as expressions. For example, the following wouldn't compile:Dim a = Dim x As String
, soCase Dim x As String
unambiguously refers to a pattern. However, even though the tuple pattern could define an expression --Dim a = (1, 2)
; because value tuples are a value type, the meaning is the same whetherCase
tuple is treated as a pattern or an expression; the ambiguity could be resolved by giving higher priority to the pattern meaning over the expression meaning.Not
is a pattern or an expression, to know ifNot
is to be a pattern or an expression. Nevertheless, I think there is enough value in the pattern to justify it.Pinging @KathleenDollard @ericmutta @paul1956
The text was updated successfully, but these errors were encountered: