- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
给定
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 a
和 B 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/
我正在尝试编写一个相当多态的库。我遇到了一种更容易表现出来却很难说出来的情况。它看起来有点像这样: {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE
谁能解释一下这个表达式是如何工作的? type = type || 'any'; 这是否意味着如果类型未定义则使用“任意”? 最佳答案 如果 type 为“falsy”(即 false,或 undef
我有一个界面,在IAnimal.fs中, namespace Kingdom type IAnimal = abstract member Eat : Food -> unit 以及另一个成功
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: What is the difference between (type)value and type(va
在 C# 中,default(Nullable) 之间有区别吗? (或 default(long?) )和 default(long) ? Long只是一个例子,它可以是任何其他struct类型。 最
假设我有一个案例类: case class Foo(num: Int, str: String, bool: Boolean) 现在我还有一个简单的包装器: sealed trait Wrapper[
这个问题在这里已经有了答案: Create C# delegate type with ref parameter at runtime (1 个回答) 关闭 2 年前。 为了即时创建委托(dele
我正在尝试获取图像的 dct。一开始我遇到了错误 The function/feature is not implemented (Odd-size DCT's are not implemented
我正在尝试使用 AFNetworking 的 AFPropertyListRequestOperation,但是当我尝试下载它时,出现错误 预期的内容类型{( “应用程序/x-plist” )}, 得
我在下面收到错误。我知道这段代码的意思,但我不知道界面应该是什么样子: Element implicitly has an 'any' type because index expression is
我尝试将 SignalType 从 ReactiveCocoa 扩展为自定义 ErrorType,代码如下所示 enum MyError: ErrorType { // .. cases }
我无法在任何其他问题中找到答案。假设我有一个抽象父类(super class) Abstract0,它有两个子类 Concrete1 和 Concrete1。我希望能够在 Abstract0 中定义类
我想知道为什么这个索引没有用在 RANGE 类型中,而是用在 INDEX 中: 索引: CREATE INDEX myindex ON orders(order_date); 查询: EXPLAIN
我正在使用 RxJava,现在我尝试通过提供 lambda 来订阅可观察对象: observableProvider.stringForKey(CURRENT_DELETED_ID) .sub
我已经尝试了几乎所有解决问题的方法,其中包括。为 提供类型使用app.use(express.static('public'))还有更多,但我似乎无法为此找到解决方案。 index.js : imp
以下哪个 CSS 选择器更快? input[type="submit"] { /* styles */ } 或 [type="submit"] { /* styles */ } 只是好
我不知道这个设置有什么问题,我在 IDEA 中获得了所有注释(@Controller、@Repository、@Service),它在行号左侧显示 bean,然后转到该 bean。 这是错误: 14-
我听从了建议 registering java function as a callback in C function并且可以使用“简单”类型(例如整数和字符串)进行回调,例如: jstring j
有一些 java 类,加载到 Oracle 数据库(版本 11g)和 pl/sql 函数包装器: create or replace function getDataFromJava( in_uLis
我已经从 David Walsh 的 css 动画回调中获取代码并将其修改为 TypeScript。但是,我收到一个错误,我不知道为什么: interface IBrowserPrefix { [
我是一名优秀的程序员,十分优秀!