如何确保Scala中的类型参数不同? [英] How to ensure that type parameters are different in Scala?

查看:131
本文介绍了如何确保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.

这篇关于如何确保Scala中的类型参数不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆