[英]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)) 这样我就可以New Map(Of Human)
,并且编译器会自动将其与Public Class Map(Of TKey, TValue)
匹配;如果我new Map(Of String)
则编译器会自动将其与Public Class Map(Of TKey As IEquatable(Of TKey), TValue)
(因为String是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 抱歉,我不会说VB ...但是在C#中(您的标签中有),您想要的是
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. 不幸的是,不支持此方法,因为约束不是签名的一部分,您将必须提供不同的签名,并且使用不同的名称(或可能使用名称空间)是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. 我个人更喜欢EquatableMap
而EqMap
,如果is
关系和重用是有益的,则EquatableMap
可以继承Map
。
.Net does not support class overloading, who knows, maybe in .Net 5.0. .Net不支持类重载,谁知道呢,也许在.Net 5.0中。 It would be useful. 这将是有用的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.