用简单的界限表达类型不等式条件

fla*_*ian 4 scala shapeless

import shapeless._

trait Something[T <: Something[T, R], R] {}
class Test[T <: Something[T, R], R, T1 <: Something[T1, _] <:!< T](t: T, t1: T1) {}
Run Code Online (Sandbox Code Playgroud)

但我得到:

type arguments [T1,?] do not conform to trait Something's type parameter bounds [T <: Something[T,R],R]
Run Code Online (Sandbox Code Playgroud)

这是有道理的,除非我希望它能起作用:

class Test1[T <: Something[T, R], R, T1 <: Something[T1, R1] <:!< T, R1](t: T, t1: T1)
Run Code Online (Sandbox Code Playgroud)

但它要求在 上具有相同的界限T1 <: Something[T, R]

我想说的是,这个类需要 4 个类型参数,每对描述 的不同后代Something[T <: Something[T, R], R]。简单地说,强制执行T != T1

这样做的正确方法是什么?

Gab*_*lla 5

一个一般的例子

import shapeless._
class Foo[A, B](a: A, b: B)(implicit ev: A =:!= B)
val x = new Foo(1, "hello")
// x: Foo[Int,String] = Foo@4015d0b9

val y = new Foo(1, 2)
// error: ambiguous implicit values:
// both method neqAmbig1 in package shapeless of type [A]=> shapeless.=:!=[A,A]
// and method neqAmbig2 in package shapeless of type [A]=> shapeless.=:!=[A,A]
// match expected type shapeless.=:!=[Int,Int]
//              new Foo(1, 2)
//              ^
Run Code Online (Sandbox Code Playgroud)

根据您的具体情况

class Test[A, B, T <: Something[T, A], T1 <: Something[T1, B]](t: T, t1: T1)(implicit ev: T =:!= T1)
Run Code Online (Sandbox Code Playgroud)