[Proposal] VB.NET Trait keyword
- Dominant language
- No language data
- Stars
- 328
- Forks
- 71
- PR merge metrics
- No merged PRs in 30d
Description
Hi, assuming that we have an interface which is named ``IExtendsSubsetList``, and a class object named ``Test1`` or a function named ``Test2`` that consuming this interface its object instance.
```vbnet
Interface IExtendsSubsetList(Of T)
Add(obj As T)
SubList(start As Integer, length As Integer)
End Interface
Public Class Test1
Public list As IExtendsSubsetList(Of T)
End Class
Public Function Test2(Of T As IExtendsSubsetList(Of XXX))(list As T)
' ... blablabla
End Function
```
So in order to construct such extends list interface its object instance, we must declare a new class object with interface implementation like:
```vbnet
Public Class ListExtendsWithSubListMethod1(Of T): Inherits List(Of T)
Implements IExtendsSubsetList(Of T)
' With A lot of redundant code, probably.
Public Overloads Sub Add(obj As T) Implements IExtendsSubsetList(Of T).Add
Call MyBase.Add(obj)
End Sub
Public Sub SubList(start As Integer, length As Integer) Implements IExtendsSubsetList(Of T).SubList
' blablabla
' implements function
End Sub
End Class
Public Class ListExtendsWithSubListMethod2(Of T): Inherits List(Of T)
Implements IExtendsSubsetList(Of T)
' With A lot of redundant code, probably.
Public Overloads Sub Add(obj As T) Implements IExtendsSubsetList(Of T).Add
Call MyBase.Add(obj)
End Sub
Public Sub SubList(start As Integer, length As Integer) Implements IExtendsSubsetList(Of T).SubList
' blablabla
' implements another function
End Sub
End Class
```
This is the only way to implements interface its object instance in current VB.NET language, **but create two class type for implements such interface probably is too much overkill, and introduce too much of the redundant code**, probably, in my personal view.
So we could introduce a new type system keyword: ``Trait``. ``Trait`` keyword is borrowing from the ``rust`` language, with ``Trait`` keyword, then we could make such interface implementation work more easily, example as:
```vbnet
Public Module Extensions
' We could do less coding when compares with the class type declare method
' makes our program code more clean
Public Sub SubListOp1(l As List(of XXX), start As Integer, length As Integer)
' blablabla
' implements function
End Sub
Public Sub SubListOp2(l As List(of XXX), start As Integer, length As Integer)
' blablabla
' implements another function
End Sub
End Module
Dim normalList As New List(Of XXX)
' In fact, the Trait keyword assembling an anonymous class type when the compiler
' lower our Trait code.
' The assembled anonymous class type implements the interface IExtendsSubsetList(Of XXX)
' and then pass its object instance to the consumer function.
Dim assemble1 As IExtendsSubsetList(Of XXX) = Trait IExtendsSubsetList(Of XXX) With {
.Add = AddressOf normalList.Add,
.SubList = AddressOf normalList.SubListOp1
}
Call Test2(assemble1)
' Assembling an anonymous class type directly at here
Call Test2(Trait IExtendsSubsetList(Of XXX) With {
.Add = AddressOf normalList.Add,
.SubList = Sub(start As Integer, length As Integer)
' anonymous method is also works under Trait
Call normalList.SubListOp2(start, length)
End Sub
})
```
+ For a tangible object type: we use ``New`` keyword for instantiate
+ But for an abstract type concepts: we could use ``Trait`` keyword for its implementation
**The ``Trait`` keyword allows us to assembling an abstract type its object instance with less coding work, and makes the object model more light weight.**
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.