简体   繁体   中英

i need a solution to do type parameter overloading

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 solution 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM