-
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] CType pattern matching operator #542
Comments
Or perhaps:
Two keywords, no parentheses, and doesn't repurpose an existing keyword for wildly different behavior:
A side benefit is avoiding the unclear mess that is the TypeOf syntax. |
Matches is a new keyword (a breaking change) in a wrong context. It refers to regular expressions and doesn't indicate types. Besides, this a bad usage of a new keyword. I would suggest: I see no deal of have two formulas of CType. It will not break anything, and we have many keywords with different usages, such as conditional If and ternary If. |
When you work with regular expressions, you write a pattern (using regular expression syntax) and check if a string matches the pattern; you can optionally capture parts of the string that match subpatterns. That is precisely the model for pattern matching (note: not just type-check-and-cast) -- does the subject of the But you seem to conflate generalized pattern matching with type-check-and-cast (as evidenced by the title of this issue). VB.NET already has a limited form of pattern matching, unrelated to type checks:
and in the context of generalized pattern matching, I think it more sensible to put the pattern before the (optional) variable introduction/extraction. |
I am against the C# type-b4-var style, period. It is not a VB style and must not be. We are not writing an essay here to think what should comes first. A programming language is a set of rules that programmers learn and use. My suggestions follows the VB.NET common sense, and this is how I would like to have this feature. In fact I hate most of C# new syntax, esp switch expression and decided not to ever use it! I will not contaminate my code with such abomination syntax. So, the only logic here is the VB.NET lang logic. We should understand the meaning from our general knowledge about the language. Matches will not satisfy that, but CType will. |
You're not alone. Eric Lippert lists the C# variable declaration style as one of the 10 worst C# features (see # 5). But We need to clarify what the goal is here:
And VB.NET already has a model for generalized pattern matching, albeit a severely limited one: the various patterns that can follow each
As noted, if the goal is a generalized pattern matching syntax, CType is almost irrelevant. |
As I said about C#, this is a kind of contaminating the language!
I have more to say in next reply: .... |
When any language wants to add a new feature, it must follow these rules:
So, this is why I hate most of pattern matching in C# and will never use them in my code. My vision of VB in all my proposals is to achieve the above goals, so any beginner takes the first look at the language finds it:
Any feature that breaks these expectation must be avoided. For example:
What can I do with two different vars in one case?
So, why we have to contaminate the language with such complexity? |
It would be better if we can use TryCast instead of CType in my proposal:
but |
What's stopping you implementing it in your own fork? |
I didn't master the source yet. It is huge. I am studying Small basic source code now, and will use it as a test lab. It is a very small lang, and I will enjoy completing it and building my VB on top of it, before tampering with Roslyn. |
【Thus】 is a good word (idea) Dim o As Object |
Dim o As Object |
Why not "As"? |
I suggest a new usage of the CType operator to be used in pattern matching like this:
The CType here returns true if obj is not nothing and the cast is possible otherwise false.
The second operand must contain a var declaration.
Comparing
CType(Obj, s As String)
(2 keys words: CType, As)to
TypeOf Obj Is s As String
(3 keys words: TypeOf, Is, As)and:
TypeOf Obj Is String Into s
(3 keys words: TypeOf, Is, Into)In my opinion, it is the most readable, meaningfull and shortest syntax possible, without the need of any new keyword.
Note: If we omit the
var As
part, we will have the old CType operation, with a possible invalid cast exception:Note also I suggest to apply the same syntax everywhere needed, such as in0-place out params:
Dim I = Integer.TryParse(x, n As Integer)
The text was updated successfully, but these errors were encountered: