dotnet / dotnet/vblang

Is Case Pattern

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

Description

In #80 @rskar-git brought up an interesting concept
**Case Function**
> Maybe instead we can invent a Case function, somewhat like we have an If function:
>
> ```vbnet
> If Case(x, 1 To 10) Then Return "Valid" Else Return "Invalid"`
> ```
> Anything that is fair game in the Case statement of a Select Case can be used:
>
> ```vbnet
> If Case(x, Is > 0) Then Return "Valid" Else Return "Invalid"
> ```
> Multiple conditions may be applicable:
>
> ```vbnet
> If Case(x, 1 To 3, 99) Then Return "Valid" Else Return "Invalid"
> ```
> (Above succeeds if between 1 and 3, or is equal to 99. As in,`(x >= 1 AndAlso x <= 3) OrElse (x = 99)`.)
>
> Perhaps two forms of Case? Single condition returns Boolean, multi condition returns Integer corresponding to the first succeeding condition (zero meaning no success)? E.g.:
> ```vbnet
> If Case(x, 1 To 3, 99) > 0 Then Return "Valid" Else Return "Invalid"
> ```
> Or instead, leave Case as a Boolean thing, and introduce Select function as the Integer thing? E.g.:
>
> ```vbnet
> If Case(x, 1 To 3, 99) Then Return "Valid" Else Return "Invalid"
> If Select(x, 1 To 3, 99) > 0 Then Return "Valid" Else Return "Invalid"
> ```

---------------------------------

With a small tweak, by permitting a single *`case-clause-statement`* appear after `object Is Case ...`
As it could work towards solving multiple issues / proposals.

* Bounds / Limits Literal #80
eg
```vbnet
If value Is Case 0 To 3, 99 Then return "Valid" Else Return "Invalid"
```
* `AnyOf` Operator #75
```vbnet
If value Is Case 0 1, 2, 3, 5, 8 Then Return "Valid" Else Return "Invalid"
```
* TypeOf over multiple types. #23
eg
```vbnet
If TypeOf obj Is Case T0, T1, T2 Then Return "Valid" Else Return "Invalid"
If TypeOf obj IsNot Case T0, T1, T2 Then Return "Valid" Else Return "Invalid"
```

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.