gpt4 book ai didi

c# - 当两个类型参数相同时,消除两个构造函数之间的歧义

转载 作者:IT王子 更新时间:2023-10-29 03:52:38 24 4
gpt4 key购买 nike

给定

class Either<A, B> {

public Either(A x) {}
public Either(B x) {}
}

当两个类型参数相同时,如何消除两个构造函数之间的歧义?

例如,这一行:

var e = new Either<string, string>("");

失败:

The call is ambiguous between the following methods or properties: 'Program.Either.Either(A)' and 'Program.Either.Either(B)'

我知道如果我给了参数不同的名称(例如 A aB b 而不仅仅是 x ),我可以使用命名参数来消除歧义(例如 new Either<string, string>(a: "") )。但我很想知道如何在不改变 Either 定义的情况下解决这个问题。 .

编辑:

可以编写一些智能构造函数,但我很想知道 Either的构造函数可以直接调用而不会产生歧义。 (或者除了这个之外还有其他“技巧”)。

static Either<A, B> Left<A, B>(A x) {
return new Either<A, B>(x);
}

static Either<A, B> Right<A, B>(B x) {
return new Either<A, B>(x);
}

var e1 = Left<string, string>("");
var e2 = Right<string, string>("");

最佳答案

How to disambiguate between the two constructors when the two type parameters are the same?

我将从不回答您的问题开始,然后以一个可以让您解决此问题的实际答案结束。

您不必这样做,因为您一开始就不应该让自己陷入这种境地。创建一个泛型类型是一个设计错误,它会导致成员签名以这种方式统一。永远不要写这样的类。

如果您返回并阅读原始的 C# 2.0 规范,您会发现最初的设计是让编译器检测泛型类型,在这种类型中以任何可能的方式来解决此类问题出现,并使类声明非法。这使其成为已发布的规范,尽管这是一个错误;设计团队意识到这条规则过于严格,因为存在以下情况:

class C<T> 
{
public C(T t) { ... }
public C(Stream s) { ... deserialize from the stream ... }
}

说这个类是非法的会很奇怪,因为你可能会说 C<Stream>然后无法消除构造函数的歧义。相反,在重载决策中添加了一条规则,该规则表示如果可以在 (Stream) 之间进行选择。和 (T where Stream is substituted for T)那么前者获胜。

因此,这种统一是非法的规则被废弃了,现在是允许的。然而,创建以这种方式统一的类型是一个非常非常糟糕的主意。 CLR 在某些情况下处理得不好,这让编译器和开发人员都感到困惑。例如,您愿意猜测这个程序的输出吗?

using System;
public interface I1<U> {
void M(U i);
void M(int i);
}

public interface I2<U> {
void M(int i);
void M(U i);
}

public class C3: I1<int>, I2<int> {
void I1<int>.M(int i) {
Console.WriteLine("c3 explicit I1 " + i);
}
void I2<int>.M(int i) {
Console.WriteLine("c3 explicit I2 " + i);
}
public void M(int i) {
Console.WriteLine("c3 class " + i);
}
}

public class Test {
public static void Main() {
C3 c3 = new C3();
I1<int> i1_c3 = c3;
I2<int> i2_c3 = c3;
i1_c3.M(101);
i2_c3.M(102);
}
}

如果你在打开警告的情况下编译它,你会看到我添加的警告,解释为什么这是一个非常非常糟糕的主意。

No, really: How to disambiguate between the two constructors when the two type parameters are the same?

像这样:

static Either<A, B> First<A, B>(A a) => new Either<A, B>(a);
static Either<A, B> Second<A, B>(B b) => new Either<A, B>(b);
...
var ess1 = First<string, string>("hello");
var ess2 = Second<string, string>("goodbye");

首先应该如何设计类Either的作者类应该写

class Either<A, B> 
{
private Either(A a) { ... }
private Either(B b) { ... }
public static Either<A, B> First(A a) => new Either<A, B>(a);
public static Either<A, B> Second(B b) => new Either<A, B>(b);
...
}
...
var ess = Either<string, string>.First("hello");

关于c# - 当两个类型参数相同时,消除两个构造函数之间的歧义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46171384/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com