Подтвердить что ты не робот

Возможно ли в Scala указать ограничение на общий тип τ такой, что τ <: σ ∧ τ ≠ σ?

У меня есть тип:

class σ

Теперь я хочу определить тип:

class υ[τ <: σ]

С дополнительным требованием, чтобы τ ≠ σ.

Возможно ли это вообще?

4b9b3361

Ответ 1

Используя Miles Sabin ответьте здесь:

trait =!=[A, B]

implicit def neq[A, B] : A =!= B = null
implicit def neqAmbig1[A] : A =!= A = null
implicit def neqAmbig2[A] : A =!= A = null

тогда:

scala> class A
defined class A

scala> class B[C <: A](implicit ev: C =!= A)
defined class B

scala> class D extends A
defined class D

scala> new B[D]()       // OK, D is a subtype of A
res4: B[D] = [email protected]


scala> new B[A]()       // Error, A =:= A
<console>:15: error: ambiguous implicit values:
   both method neqAmbig1 of type [A]=> =!=[A,A]
   and method neqAmbig2 of type [A]=> =!=[A,A]
   match expected type =!=[A,A]

scala> class E
defined class E

scala> new B[E]()       // Error, E is not a subtype of A
<console>:15: error: type arguments [E] do not conform to class B type parameter bounds [C <: A]