dotnet / dotnet/vblang

<Flags> Enum operators

Open
#520 21 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

## Enum Operators
Operating with `Enums` can be a point a pain, so I've been thinking for quite a while on have the following operators. That expand the capabilities of the dictionary lookup operator, eg does the variable have the flag `IsShared` set? We are looking up the flag(s).

* `source ! flaganame` and `source !( flagsEnumExpression )`
* Are all of the matching flags set in source.
* `source !? flagname` and `source !?( flagsEnumExpression )`
* Are any of the matching flags set is source.
* `source !+ flagname` and `source !+ ( flagsEnumExpression )`
* Set the matching flags in source.
* `source !- flagname` and `source !- ( flagsEnumExpression )`
* Clear the matching flags in source.

The the converted to the calls or implementation of the corresponding methods. This also allows compile-time constant folding to happen. As well compile-time validation of the flagname.
```vbnet
Function AllSet(Of T As Enum)(source As T, matching As T) As Boolean
Return (source And matching) = matching
End Function

Function AnySet(Of T As Enum)(source As T, matching As T) As Boolean
Return (source And matching) <> 0
End Function

Function SetFlags(Of T As Enum)(source As T, matching As T) As T
Return (source Or matching)
End Function

Function ClearFlags(Of T As Enum)(source As T, matching As T) As T
Return (source And (Not matching))
End Function
```

### Implementation

The expression are to represented via a `FlagsEnumExpression` that supports the above operators,
an implementation detail there will be a "effective direct cast conversion" from the textual member (eg the flag's name) to a `FlagsEnumExpression` (eg flags value).
`Enum -> FlagsEnumExpression`

Note we'll require some smarts in the compiler to differeniate between the type character ` singlevariable! ' Of Type Single` and that of these operators.

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.