Composable types?
- Dominant language
- No language data
- Stars
- 328
- Forks
- 71
- PR merge metrics
- No merged PRs in 30d
Description
When we declare a generic ``Function/Type``, we can compose the generic type constraint from multiple types, for example:
```vbnet
' declare a generic type with composable type constraint
Public Class Foo(Of T As {IComparable(Of T), IEnumerable(Of String)})
' ....
End Class
' or declare a generic function with composable type constraint
Public Sub Foo(Of T As {IComparable(Of T), IEnumerable(Of String)})(obj As T)
End Sub
```
Recently, a serials of type pattern programming feature was proposed by @AdamSpeight2008:
+ **Is Case Pattern #160**
+ **[WIP] `AnyOf` Operator #75**
+ **[Proposal] TypeOf over multiple types. #23**
All these issue are talking about the type match in pattern way. And what about produce object from such type pattern? @zspitz proposal declare a **composable type at this [issue](https://github.com/dotnet/vblang/issues/47)** for such type pattern generation, but the syntax is too much complicated. And in my opinion, for declare a composable type, the declare statement format can keeps the same as the generic type constraint:
```vbnet
Dim obj As {IComparable(Of T), IEnumerable(Of String)}
```
## Advantage
1. Improvements on the coding experience, less type casting, less coding, save our time
2. Safer than using ``Object`` type
3. Especially helpful on **Factory Design Pattern**
##### Using Object
```vbnet
Public Function Factory(type As type) As Object
' template type for produce the factory product
' should implements all of the interface type andalso
' inherits from a specific base type.
If Not type.IsInheritsFrom(GetType(OtherType)) OrElse
Not {
GetType(IComparable(Of String)),
GetType(IEnumerable(Of Char)),
GetType(IList(Of Char()))
}.All(Function(t)
Return type.ImplementInterface(t)
End Function) Then
Return Nothing
End If
Dim obj As Object = Activator.CreateInstance(type)
' In current version of VisualBasic, obj can not declared as a composable type,
' so that the type casting is required!
' too much inconvenience
Call DirectCast(obj, OtherType).BlaBlaBla()
Call DirectCast(obj, IList(Of Char())).AddRange("1234")
' blabla
Return obj
End Function
```
##### Using Composable type
```vbnet
Public Function Factory(type As type) As Object
' template type for produce the factory product
' should implements all of the interface type andalso
' inherits from a specific base type.
If Not type.IsInheritsFrom(GetType(OtherType)) OrElse
Not {
GetType(IComparable(Of String)),
GetType(IEnumerable(Of Char)),
GetType(IList(Of Char()))
}.All(Function(t)
Return type.ImplementInterface(t)
End Function) Then
Return Nothing
End If
Dim obj As {
OtherType,
IComparable(Of String),
IEnumerable(Of Char),
IList(Of Char())
} = Activator.CreateInstance(type)
' due to the reason of obj is a composable type,
' so that the type casting operation will no longer required.
Call obj.BlaBlaBla()
Call obj.AddRange("1234")
' blabla
Return obj
End Function
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.