Skip to content
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

[Proposal] Select Case ? pattern #598

Open
VBAndCs opened this issue Jun 22, 2021 · 3 comments
Open

[Proposal] Select Case ? pattern #598

VBAndCs opened this issue Jun 22, 2021 · 3 comments

Comments

@VBAndCs
Copy link

VBAndCs commented Jun 22, 2021

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.

@paul1956
Copy link

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.

@VBAndCs
Copy link
Author

VBAndCs commented Jun 24, 2021

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.

@franzalex
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants