-
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
[Proposal] Pattern matching using Select TypeOf #541
Comments
I have a few issues with this:
I think it would be helpful if you could clarify what benefits this syntax has over one of the type-checking patterns mentioned in #367. |
Think of Type as a verb not a none: |
I think it's only because you associate declaring a variable with But consider -- the main point of the pattern is not the variable declaration, but the type check; the variable declaration follows from the type check -- "if To emphasize this, how would you express a type-check without a variable declaration? With
But if you insist on using some form of
Also, |
The meaning is obvious in both English and VB. Besides, we use exactly the same syntax today: |
Sorry: |
I choose to use |
Same as my suggestion, and I see it better. I can drop type and use case, and it will give the same meaning:
VB uses the variable first in almost all places, and this should always be the case. |
Especially in vb.net context is king. Your syntax blur the meaning of a declaraion and a a type check., how would some learning the language understand the different semantic meaning? |
As we did when we learned |
Since When reading or understanding the code it is going artificially bias the as being more important (aka it has an higher order of precedence). thus changes the meaning from In a catch statement If we continue down the rabbit hole, I'd also expect to also to validly write, since I can currently do that when I write a declaration. |
Still see no issue. In fact, we need VB to allow declaring variables in place everywhere, such as: |
Agreed. When the primary purpose of a construct is to declare a variable, But for pattern matching syntax, the primary purpose is not declaring a variable; it's matching against a pattern. The variable declaration is secondary to the pattern being matched. And you still haven't clarified what benefit there is in |
This is exactly the primary purpose of type matching: casting type and assigning it to a var. Otherwise, we already match patterns for ever, but need further steps to assign them to vars. |
Absolutely, using
such as
But this is not the primary purpose of pattern matching. Constraining pattern matching to only type matching, and to only capturing the entire subject of the Admittedly, type-checking-and-variable-population is the most compelling use of pattern matching, but it is certainly not the only one. Also, if the whole intention is just to satisfy this use case (of type matching and populating a variable), why should we need a new variable? #172 is a better choice -- redefine the type of the current variable/expression within the |
If I look at several large databases of VB code I see 2 things over and over
and
|
@paul1956 The first code snippet is covered by #172, making the following code valid:
For the second snippet, which is preferable? This:
Or:
|
I prefer the second more VB like. How does C# handle the writeline(x) in the else I believe you can't access X and it is usually a throw? I would be fine if below worked without the warning when the parameter is declared . VB already support but from what I have seen it is ignored.
For the Select Case or If the concept of the type changing in the scope of the Block is very interesting but it may be confusing to someone reading the code. |
I think it's a compilation error, and I propose the same for VB.NET. In the following code:
what should be the type of
You always need some context to understand the types used by a piece of code:
And in this case the precise type can be seen in the enclosing block. |
Based on #542 , I suggest this :
|
Again, why do you think the start of the
which is something you can't do if you insist on |
You can't compare with values unless you are certain of the Var type. Your example doesn't make any sense. |
Yes, you're right. The |
This still messy. At least it will not work when Option Strict is On. I see no practical using worth concerning for mixed values and pattern match in the Select Case. |
It depends what the goal is, as I noted here. If the goal is generalized pattern matching, then there's no reason why this shouldn't be allowed even under |
It helpful if thing about what the lower form of the select - case with "patterns", will tend to be Module HelperFunctions
Public Function TypeOfIs(Of T As Class)( source As Object, ByRef output As T) As Boolean
source = TryCast(source, T)
Return source IsNot Nothing
End Function
Public Function TypeOfIs(Of T As Structure)( source As Object, ByRef output As T) As Boolean
Dim temp = DirectCast(source, T?)
output = temp.GetValueOrDefault()
Return temp.HasValue
End Function
End Function Dim result As TSomething = Nothing
IF Helpers.TypeOfIs(Of TSomething)( expr, result ) Then |
The |
This is a deep trap in your design. What if I need to use an already existing var? |
The prototype is a work in progress, to get the general concept across and try out how it feels. Already thought about that
|
@VBAndCs I want to get some attention but it is all over the place. I would like to model it after C# DeclarationPatternSyntax.
Becomes below with new feature
Above might not be acceptable syntax but it is clear what it is doing. |
@paul1956 what about Select Case x
Case TypeOf x is Something Into y ' possible syntax
' y is a new variable of type Something and it's value = x
End Select
If TypeOf x is Something Into y Then
' y is a new variable of type Something and it's value = x
End If |
@AdamSpeight2008 I like it, it is clear to me what is happening much better then what I thought of. Also it would be very easy to do a CodeFix. |
What is the purpose of |
I think the perfect syntax can result from combining the Anthony's proposal with mine, so, we need to declare no new variables to deal with the target type. The Select TypeOf O
Case Nothing
Console.WriteLine("Nothing")
Case String
Console.WriteLine(O[0])
Case Date
Console.WriteLine(O.ToShortDateString( ))
End Select Which can be lowered to: If O is Nothing Then
Console.WriteLine("Nothing")
ElseIf TypeOf O is String Then
Dim O1 = CType(O, String)
Console.WriteLine(O1[0])
ElseIf TypeOf O is Date Then
Dim O2 = CType(O, String)
Console.WriteLine(O2.ToShortDateString())
End Select which can avoid any complications in Anthony's proposal. |
I suggest this syntax for pattern matching in select statements:
Edit:
Which can be lowered to:
Based on #542 , I suggest this:
The text was updated successfully, but these errors were encountered: