Skip to content

Overload resolution to take into account Generic Parameters. #177

@AdamSpeight2008

Description

@AdamSpeight2008

Overload resolution should bind to the overload that is most specific including generic type parameters (constrained or not)

Sub Foo(Of T As Struct)( Bar As T) // 0
Sub Foo(Of T As Class)( Bar As T) // 1

If Foo is called with an integer, then method 0 is used.
If Foo is called with a string, then method 1 is used.

This would allow the methods to me specialised for that type of object. For example the difference is the null checks in the case of a class type.

Public Function IsBetween(Of T As {class, IComparable(Of T) } )
  ( value As T,
    lower as T,
    upper as T
  ) As Boolean
  If value Is Nothing Then Throw New NullArgumentException()
  If lower Is Nothing Then Throw New NullArgumentException()
  If upper Is Nothing Then Throw New NullArgumentException()
  Return _IsBetween(Of T)( value, lower, upper )
End Function

Public Function IsBetween(Of T As {class, IComparable(Of T) } )
  ( value As T,
    lower as T,
    upper as T
  ) As Boolean
  Return _IsBetween(Of T)( value, lower, upper)
End Function

Private Function _IsBetween(Of T As IComparable(Of T))
  ( value As T,
    lower As T,
    upper As T
  ) As Boolean
      Return (lower.ComparedTo(value) <= 0) AndAlso
     (value.ComparedTo(upper) <= 0) 
End Function

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions