dotnet / dotnet/vblang

[Proposal] CType pattern matching operator

Open
#542 13 comments 0 reactions 0 assignees View on GitHub
Dominant language
No language data
Stars
328
Forks
71
PR merge metrics
No merged PRs in 30d

Description

I suggest a new usage of the CType operator to be used in pattern matching like this:
```VB.NET
If CType(Obj, s As String) AndAlso s.Lenght > 0 then

End If
```

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:
```VB.NET
If CType(Obj, String).Lenght > 0 then

End If
```

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)`

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.