如何确保Scala中的类型参数不同?

如何确保Scala中的类型参数不同?

问题描述:

使用以下定义可以确保具体的类型参数是相等的:

With the following definition it's possible to ensure the concrete type parameters are equal:

trait WithEqual[T1 >: T2 <: T2, T2]

所以行

type A = WithEqual[Int, Int]

将是合法的。现在我的问题是:如何实现恰恰相反?因此,下面的代码行不应该被编译:

will be legal. Now my question is: How to achieve exactly the opposite? Thus, the following line should not compile:

type B = WithUnequal[Int, Int]


这在Scala中非常棘手,您必须求助于故意含糊不清。以下是示例

This is pretty tricky in Scala, you have to resort to intentional ambiguity. Here's an example of this technique.

马克Harrah在他的操场上推广了类型级hackery的技巧,向上

Mark Harrah has generalized the trick in his playground for type-level hackery, Up.

这可能适用于您的问题,但我现在没有时间尝试。

It's likely that this could be applied to your question, but I haven't the time to try right now.