ryskajakub
ryskajakub

Reputation: 6431

Scala type level minus

Is there a way, how to express minus operator on types? I'd like to be able to achieve this:

trait A
trait B
type C = A with B
type D = C minus A
// type B == D

I know, that this cannot be done directly, but maybe someone was able to express this operator with some clever trick...

--- edit

Motivation:

trait A
object B extends A
object C extends A
object D extends A
def myMethod(one:A,two:A,three:A) = ???

It is only valid to use every type once, so when the user of this method picks B for one parameter, it is valid to use C and D in the other parameters...

Upvotes: 3

Views: 235

Answers (1)

Jean-Philippe Pellet
Jean-Philippe Pellet

Reputation: 59994

There are two different questions — I'm only answering the use case where you want a call to myMethod to fail if you pass the same object twice. For the first four lines, see Miles Sabin’s answer to the question that Peter Schmitz linked.

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

trait A
object B extends A
object C extends A
object D extends A
def myMethod[A, B, C](one: A, two: B, three: C)(implicit ev1: A =!= B, ev2: A =!= C, ev3: C =!= B) = ???

Upvotes: 2

Related Questions