flavian
flavian

Reputation: 28511

Expressing type inequality conditions in a simple bound

import shapeless._

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

but I get:

type arguments [T1,?] do not conform to trait Something's type parameter bounds [T <: Something[T,R],R]

Which makes sense except I would expect this to work:

class Test1[T <: Something[T, R], R, T1 <: Something[T1, R1] <:!< T, R1](t: T, t1: T1)

But it's requesting the same bound on T1 <: Something[T, R].

What I want is to say this class takes 4 type arguments, each pair describing a different descendent of Something[T <: Something[T, R], R]. Simple put, enforce that T != T1.

What is the correct way to do that?

Upvotes: 4

Views: 171

Answers (1)

Gabriele Petronella
Gabriele Petronella

Reputation: 108101

A general example

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)
//              ^

in your specific case

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

Upvotes: 5

Related Questions