Is your feature request related to a problem?
Yes. Suppose we're writing a generic container, say a List (array):
Class ListBase(Of T)
...
End Class
This base cannot implement all methods, since some of them depend on whether the type is an object or not (string, UDT, ...).
Then we end up with a ZOO of classes:
Class NonComparableList(Of T) ' T is not an object, and it cannot be compared for whatever reason
Class List(Of T) ' T is not an object, and can be compared
Class NonComparableObjectList(of T) ' T is an object, we don't want comparisons to be available
Class IsComparableObjectList(of T) ' T is an object, comparison by instance pointer (Is)
Class ValueComparableObjectList(of T) ' T is an object, comparison by value using a dedicated method, e.g. "EqualsTo" (as long as `Object_Compare` method is not a thing)
Describe the solution you'd like
This ZOO could be prevented with introduction of a [SFINAE] attribute, as follows:
-
[SFINAE] Methods that cannot compile due to typecheck errors don't cause compilation errors unless they get referenced (used).
-
[SFINAE(priority) methods can have same signature as long as priority differs. When such a method is referenced, they are type-substituted in order of increasing priority. The first one for which the substitution succeeds is selected. If no substitution succeeds, a compilation error results.
If the [SFINAE] attribute is undesirable, all methods in generic types can be considered as-if they have been declared [SFINAE(priority)], with priorities increasing in definition order. The first successful substitution in definition order is chosen. Multiple possible substitutions are not an error.
Examples:
[DefaultMember]
Public Property Get Item(ByVal index%) As T
Return mItems(index)
End Property
[DefaultMember] [SFINAE]
Public Property Let Item(ByVal index%, value As T)
' valid for non-object types, and object types that support assignment
Let mItems(index) = value
End Property
[DefaultMember] [SFINAE]
Public Property Set Item(ByRef index%, value As T)
' valid for object types only
Set mItems(index) = value
End Property
[SFINAE(1)]
Public Sub Swap(ByVal a%, ByVal b%)
' valid for object T only
Dim temp As T
Set temp = Me(a)
Set Me(a) = Me(b)
Set Me(b) = temp
End Sub
[SFINAE(2)]
Public Sub Swap(ByVal a%, ByVal b%)
' valid for non-object T
Dim temp As T = Me(a)
Me(a) = Me(b)
Me(b) = temp
End Sub
[SFINAE(1)]
Public Function IndexOf%(item As T)
' Valid for comparable types
For i As integer = 1 To Count
If Me(i) = item Then
Return i
End If
Next i
Return -1
End Function
[SFINAE(2)]
Public Function IndexOf%(item As T)
' Valid for object types
For i As integer = 1 To Count
If Me(i) Is item Then
Return i
End If
Next i
Return -1
End Function
Describe alternatives you've considered
Free generic functions are an alternative, since unless they are invoked, they don't get substituted. However, the syntax becomes a bit clumsy, since Object.Method notation cannot be used unless #2296 gets implemented.
Is your feature request related to a problem?
Yes. Suppose we're writing a generic container, say a List (array):
This base cannot implement all methods, since some of them depend on whether the type is an object or not (string, UDT, ...).
Then we end up with a ZOO of classes:
Describe the solution you'd like
This ZOO could be prevented with introduction of a
[SFINAE]attribute, as follows:[SFINAE]Methods that cannot compile due to typecheck errors don't cause compilation errors unless they get referenced (used).[SFINAE(priority)methods can have same signature as long as priority differs. When such a method is referenced, they are type-substituted in order of increasing priority. The first one for which the substitution succeeds is selected. If no substitution succeeds, a compilation error results.If the
[SFINAE]attribute is undesirable, all methods in generic types can be considered as-if they have been declared[SFINAE(priority)], with priorities increasing in definition order. The first successful substitution in definition order is chosen. Multiple possible substitutions are not an error.Examples:
Describe alternatives you've considered
Free generic functions are an alternative, since unless they are invoked, they don't get substituted. However, the syntax becomes a bit clumsy, since
Object.Methodnotation cannot be used unless #2296 gets implemented.