Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ensure that type parameters are different in Scala?

Tags:

generics

scala

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

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

So the line

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]
like image 268
karlf Avatar asked Dec 29 '22 06:12

karlf


1 Answers

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

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.

like image 160
retronym Avatar answered Mar 25 '23 05:03

retronym