开发者

i need a solution to do type parameter overloading

开发者 https://www.devze.com 2023-03-07 17:58 出处:网络
I need type parameter overloading as such: Public Class Map(Of TKey, TValue) Public Class Map(Of TKey As IEquatable(Of TKey), TValue)

I need type parameter overloading as such:

Public Class Map(Of TKey, TValue)

Public Class Map(Of TKey As IEquatable(Of TKey), TValue)

so that i can New Map(Of Human) and the compiler will automatically match it to Public Class Map(Of TKey, TValue) and if i new Map(Of String) the compiler will automatically match it to Public Class Map(Of TKey As IEquatable(Of TKey), TValue) (since String is an IEquatable(Of String))

Right now my solutio开发者_C百科n is to give the class different names as such:

Public Class Map(Of TKey, TValue)

Public Class EqMap(Of TKey As IEquatable(Of TKey), TValue)

But I'm looking for a better solution


Sorry I don't speak VB... but in c# (which you have in your tags) what you want is

Map<TKey,TValue>
{
    // implementation
}

and

Map<Tkey,TValue>
   where TKey: IEquatable
{

}

unfortunately this isn't supported because constraints are not part of the signature you will have to provide different signatures and using different names (or possibly namespaces) is the cleanest solution IMO.


There is no solution, better than what you propose. Personally I would prefer EquatableMap to EqMap, EquatableMap could inherit Map if the is relationship and reuse was beneficial.

.Net does not support class overloading, who knows, maybe in .Net 5.0. It would be useful.

0

精彩评论

暂无评论...
验证码 换一张
取 消