dotnet / dotnet/vblang

Roundup of proposed patterns on vblang

Open
#367 8 comments 5 reactions 0 assignees View on GitHub
Dominant language
No language data
Stars
328
Forks
71
PR merge metrics
No merged PRs in 30d

Description

As a follow-up to https://github.com/dotnet/vblang/issues/337#issuecomment-448427815, this is a collection of proposed patterns described in the various issues related to pattern matching (#124 and #337).

@AnthonyDGreen's patterns:

| Source | Name | Suggested syntax | Recursive | Comments |
|--------|----------|--------------------------------------------------------------|-----------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| #124 | wildcard | `*` | | Matches all values, including `Nothing` |
| #124 | variable | _identifier_ [`?`] [ `As` _typename_] | | Introduces a variable
The variable's type is that of the value to be matched, if not specified in the pattern
Without `?`, match fails on `Nothing`
With `?`, match succeeds on `Nothing`; the variable's type is nullable |
| #124 | function | _identifier_ `(` [ _pattern1_ [`,` _pattern2_ ...] ] `)` | Yes | _identifier_ is a Boolean-returning function, with 0 or many parameters
Sub-patterns can be used in place of `ByRef` parameters, whose value must match the corresponding pattern
Allows extending pattern matching (AKA F# active patterns) |
| #124 | tuple | `(` _pattern1_ [ `,` _pattern2_ ...] `)` | Yes | Matches a tuple of arity eual to the number of patterns specified, if each of the sub-patterns also match |
| #141 | array | `{` _pattern1_ [ `,` _pattern2_ ...] `}` | Yes | Matches an array with the specified number of elements, if each of the sub-patterns also match |
| #140 | string | _interpolated string_ | Yes | Matches if the string can be deconstructed using the constant parts of the interpolated string
Interpolations are treated as sub-patterns which the variable strings are matched against |
| #139 | XML | _XML literal_ | Yes | Matches an `XElement` if the `XElement` can be deconstructed based on the constant parts
Embedded expressions are treated as sub-patterns, which are matched against corresponding content |
| #124 | JSON | | Yes | Using some syntax similar to JSON literals in #101 |

Note: This set of patterns assumes the use of `Matches` (or some other dedicated keyword) in `Case` clauses.

---

@bandleader / @zspitz 's patterns:

| Source | Name | Suggested syntax | Recursive | Comments |
|-|-|-|-|-|
| [#337](https://github.com/dotnet/vblang/issues/337#issuecomment-448427815) | expression | _expression_ | | Any arbitrary expression1 |
| [#337](https://github.com/dotnet/vblang/issues/337#issuecomment-448427815) | comparison | [`Is`] _comparison-operator_ _expression_ | | Matches if the comparison holds1 |
| [#337](https://github.com/dotnet/vblang/issues/337#issuecomment-448427815) | like | `Like` _string-expression_ | | Matches if the value is `Like` the specified string expression1 |
| [#337](https://github.com/dotnet/vblang/issues/337#issuecomment-448427815) | range | _expression_ `To` _expression_ | | Matches if the value is between the two expressions1 |
| [#337](https://github.com/dotnet/vblang/issues/337#issuecomment-448427815) | variable + typecheck | `Dim` _identifier_ [`As` _typename_] | | Introduces a variable into the pattern expression's child scope
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 hold `Nothing`2 |
| [#337](https://github.com/dotnet/vblang/issues/337#issuecomment-448427815) | typecheck | `As` _typename_ | | Matches if the value is of the given type
A value of `Nothing` matches if _typename_ can hold `Nothing`2 |
| [#337](https://github.com/dotnet/vblang/issues/337#issuecomment-448427815) | OR | _pattern_`,` _pattern_ | Yes | Matches if either sub-pattern matches1 |
| [#124](https://github.com/dotnet/vblang/issues/124#issuecomment-416292181) | tuple | `(` _pattern_ [`,` _pattern_ ...] `)` | Yes | Matches a tuple with the specified number of elements, and each element matches the corresponding sub-pattern3 |
| [#124](https://github.com/dotnet/vblang/issues/124#issuecomment-416292181) | NOT | `Not` _pattern_ | Yes | Matches only if the value doesn't match the sub-pattern4 |
| [#124](https://github.com/dotnet/vblang/issues/124#issuecomment-439357073) | with | `With {.` _identifier_ `=` _expression_ ... `}`


`With {.` _identifier_ `Matches` _pattern_ ... `}` | Yes | Matches if the value has public properties/fields with a property / public field whose value is equal to _expression_, or that matches _pattern_
Multiple properties/fields can be tested against |

Footnotes:

1. This set of patterns assumes `Case` _pattern_, without a dedicated keyword. Therefore, compatibility with existing `Case` syntax needs to be preserved.
2. Hopefully, VB.NET will get non-nullable reference types; they can be used to exclude `Nothing` from reference types
3. Because VB.NET supports `Case` _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`, so `Case 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 whether `Case` _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.
4. See the previous note. It will be necessary to determine if what follows the `Not` is a pattern or an expression, to know if`Not` 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

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.