vblang icon indicating copy to clipboard operation
vblang copied to clipboard

[Proposal] Select Case ? pattern

Open VBAndCs opened this issue 4 years ago • 3 comments

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.

VBAndCs avatar Jun 22 '21 12:06 VBAndCs

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.

paul1956 avatar Jun 24 '21 03:06 paul1956

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 avatar Jun 24 '21 07:06 VBAndCs

@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'.

franzalex avatar Jun 24 '21 07:06 franzalex