vblang
vblang copied to clipboard
[Proposal] Select Case ? pattern
I suggest to allow to composing the select case expr as a pattern using ? like this:
Dim X = "a test"
Select case X.StartsWith(?)
Case "a", "b"
Case "c"
Case Else
End Select
It will be lowered to:
If X.StartsWith("a") OrElse X.StartsWith("b")
ElseIf X.StartsWith("c")
Else
End if
I also suggest to allow using members of the Select expr in the cases:
Select Case X
Case "test"
Case .StartsWith("a")
Case Like "*s*"
Case .Contains("a")
Case .Substring(1, 2) = "ab"
End Select
the case will considered to have an embedded with applied on the case expr only not its body, and it will have effect over any outer with. The above syntax can be lowered to if statement.
All of those things can easily be done in VB today with Select Case True and StartsWith is the same as X(0) = "?" where ? is the thing you want to match against.
Select case True is an illusion verbose version of If ElseIf and it defies the purpose of Select case as a shorter version of If.
@VBAndCs
You're right that Select Case True is essentially a rewrapped If ... Then ... ElseIf ... End If block.
In fact, unlike C#'s switch statement (before pattern matching came in), most Select Case statements in VB.NET actually get converted to If ... Else statements.
That considered, your proposal is also a repackaged If ... Else block. It will be useful if you can state how your suggestion improves the language in any way without being 'another way to do the same thing'.