It's very common in the dotnet/roslyn-analyzers repo to have language-specific code to handle Syntax nodes.
In C#, it's pretty easy to cast an object inside a condition:
if (myOriginalInstance is MyType myCastedInstance)
{
...
But in VB, the casting is a bit more elaborated:
Dim myCastedInstance As MyType = TryCast(myOriginalInstance, MyType)
If (myCastedInstance IsNot Nothing Then
...
In the VB example, the left part of the definition of myCastedInstance
does not need to have the data type of the variable specified. The data type can be inferred from the TryCast
, since the second argument of that operator is the data type to cast into.
So that code can be simplified to:
Dim myCastedInstance = TryCast(myOriginalInstance, MyType)
vb
Dim variableName
vb
Dim variableName As MyType
TryCast
or a DirectCast
operator invocation.vb
Dim variableName As MyType = TryCast(originalVariableName, MyType)
TryCast
in step 3.vb
Dim variable = TryCast(originalVariableName, MyType)
IMO this needs to be moved to roslyn repo and be handled as part of the useless code.
I can work on this if the team accepts the proposal.
I can work on this if the team accepts the proposal.
I would champion this. Note: this would not just be for TryCast/DirectCast, but would be similar to the 'var' rules for C# where you could say:
these cases would fall under "when variable type is apparent" bucket
Should be moved to VBLang.
It is also disingenuous and disrespectful to other VBLang proposals, when they've been informed the VB.net is receiving any more updates (including tooling)
Should be moved to VBLang.
@AdamSpeight2008 This is not a VBLang issue.
It is also disingenuous and disrespectful to other VBLang proposals, when they've been informed the VB.net is receiving any more updates (including tooling)
I don't know what you're talking about. We are constantly working on VB. As i said, I would support this and will bring to the next design meeting to be done. If you would like to discuss this more, you can come to gitter/discord and discuss it with me. Other comments about this on this issue will be treated as off-topic and moderated as such.
It simplifies to Dim myCastedInstance = TryCast(myOriginalInstance, MyType)
, if Option Infer On
is set, otherwise it changes the type of myCastedInstance
to Object
and not MyType
. That behaviour can not change for compatibility reasons.
It simplifies to Dim myCastedInstance = TryCast(myOriginalInstance, MyType), if Option Infer On
Yup. That's why I support this and would champion this.
That behaviour can not change for compatibility reasons.
Just like with 'use var' we would not offer if it changed semantics.
Most helpful comment
I would champion this. Note: this would not just be for TryCast/DirectCast, but would be similar to the 'var' rules for C# where you could say:
these cases would fall under "when variable type is apparent" bucket