[英]Constrain method type argument to be base of class generic type
I would like to ensure that method type argument is base type of class generic type so first I naturally wrote this: 我想确保方法类型参数是类通用类型的基本类型,因此首先我自然地这样写:
public class FivePM<T> {
public void drink<M>(M x) where T : M {}
}
Is there any specific reason why this can't work? 是否有任何特定原因导致其无法正常工作?
Background 背景
I wrote container class Bag<B>
which stores items shared between different instances, so an item is automatically removed from one bag if it's added to another. 我写了容器类Bag<B>
来存储在不同实例之间共享的项目,因此,如果将一个项目添加到另一个袋子中,则会自动将其从一个袋子中删除。 This is done internaly by the BagSet<BT>
which hold bags for common items. 这是由BagSet<BT>
在内部完成的,该BagSet<BT>
保存普通物品的袋子。 I wanted for bag generic type to be the lowest common type of items in sets but don't want constrian B
to be exactly BT
but to any derived type. 我希望袋子通用类型是集合中项目的最低通用类型,但我不希望constrian B
完全是BT
而是任何派生类型。 I've managed to make type safe public interface for the bag fulfill my requirements but because of generic constrains limitations, bags construction looks awkward and I can't use list initializer: 我设法使袋子的类型安全的公共接口满足了我的要求,但是由于通用约束的限制,袋子的构造看起来很尴尬,我不能使用列表初始化程序:
BagSet<object> bset = new BagSet<object>();
Bag<int> suitcase = bset.newBag<int>();
public class BagSet<T> : BagSetBase {
public Bag<B> newBag<B>(string name = null, params B[] items) where B : T {
var b = new Bag<B>(this, name);
for (int i = 0; i < items.Length; i++) b.Add(items[i]);
return b;
}
}
Are generic constrains going to be improved someday? 通用约束是否有一天会得到改善? Maybe I should wait before making such things extensively. 也许我应该等待,然后再广泛地做这些事情。
Using your real code, what if you made class Bag
take two types and handle the inheritance requirement - after all, you don't care about that in a BagSet
: 使用您的真实代码,如果使class Bag
成为两种类型并处理继承要求,该怎么办-毕竟,您不必在BagSet
关心它:
public class BagSet<T> {
public Bag<B, T> newBag<B>(string name = null, params B[] items) where B : T {
var b = new Bag<B, T>(this, name);
for (int i = 0; i < items.Length; i++) b.Add(items[i]);
return b;
}
}
public class Bag<B, T> where B : T {
BagSet<T> common;
string bsname;
public Bag(BagSet<T> bs, string name) {
common = bs;
bsname = name;
}
public void Add(B item) {
}
}
Then you can declare them like so: 然后,您可以像这样声明它们:
var bset = new BagSet<object>();
var suitcase = bset.newBag<int>();
Is there any specific reason why this can't work? 是否有任何特定原因导致其无法正常工作?
If you mean is there a logical reason why that kind of constraint doesn't make sense, then no. 如果您的意思是说这种约束没有道理是合乎逻辑的 ,那就没有。 That's a perfectly sensible constraint, and there are languages that support that sort of constraint. 这是一个完全明智的约束,并且有些语言支持这种约束。 Java, for instance. 以Java为例。 And Scala. 和斯卡拉。
If you mean is there a reason why this doesn't work in C# , that's easy. 如果您的意思是为什么在C#中不起作用,那很简单。 No one ever implemented that feature in C#. 没有人在C#中实现该功能。 In order for a feature to work in C#, someone has to think of it, design it, write a specification, implement the specification, write tests, and then ship it to customers. 为了使功能可以在C#中工作,必须有人考虑,设计,编写规范,实施规范,编写测试,然后将其交付给客户。 Of those necessary steps, only the first one happened. 在这些必要步骤中,只有第一个发生了。
Are generic constrains going to be improved someday? 通用约束是否有一天会得到改善?
Questions asking for a prediction of the future are off-topic on Stack Overflow. 要求对未来进行预测的问题不在Stack Overflow上的话题了。 We have no ability to reliably predict the future. 我们没有能力可靠地预测未来。
If you'd like this feature to be in a future version of C#, consider advocating for it on the github forum. 如果您希望此功能成为C#的未来版本,请考虑在github论坛上倡导该功能。
I think you might be able to get this working: 我认为您也许可以使此工作正常:
public class FivePM<T, M> where T : M
{
public void drink(M x)
}
But then again your question confused me and it is past midnight here so I might be wrong and I might misunderstood the question.... Or maybe not? 但话又说回来,您的问题使我感到困惑,并且已经过了午夜,所以我可能错了,我可能会误解了这个问题...。也许不是吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.