Numeric Constraint for Generics
- Dominant language
- No language data
- Stars
- 328
- Forks
- 71
- PR merge metrics
- No merged PRs in 30d
Description
## Proposal
Add a new Attribute / Constraint to Generic Parameters to restrict the type to those that are considered `numeric`. *How to define what is numeric is another discussion.*
### Definition of "Numeric"
For the purpose of this proposal we are using the definition as define in the grammar as `NumericTypeName`
[Spec: Primitive Types](https://github.com/dotnet/vblang/blob/783a91098f951e07b6eefd088ef0a59b880ff31b/spec/types.md#primitive-types)
### Benefits
Since we now know the type is constrained to be numeric, we can permit the usage of operator within the generic operator being defined.
```vbnet
Structure Range(Of Tx, Sx )
Public ReadOnly X As Tx
Public ReadOnly Y As Tx
Public ReadOnly S As Sx
Public Sub New ( X As Tx, Y As Tx, S As Sx)
Me.X = X : Me.Y = Y : Me.S = S
End Sub
Public Function GetEnumerator() As IEnumerable(Of Tx)
Return If( X <= Y , Ascending(), Descending() )
End Function
Private Iterator Function Ascending() As IEnumerable(Of Tx)
Dim Current = X
Dim Last = Y - S
While Current <= Last
Yield Current
Current += S
End While
Yield Current
End Function
Private Iterator Function Ascending() As IEnumerable(Of Tx)
Dim Current = X
Dim Last = Y - S
While Current >= Last
Yield Current
Current += S
End While
Yield Current
End Function
End Structure
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.