Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Evidence that types are not equal in Scala [duplicate]

Is there any way to constraint a method so that it only makes sense if two types are proved not to be equal?

trait Something[A, B] {
  // I can only be called if type A is the same as type B
  def ifEqual(implicit ev: A =:= B)

  // Now I cannot be called if type A is proven to be the same as type B
  def ifNotEqual(implicit ev: A ??? B)
}
like image 366
Hugo Sereno Ferreira Avatar asked Dec 12 '22 05:12

Hugo Sereno Ferreira


1 Answers

Yes. From shapeless,

// Type inequalities
trait =:!=[A, B] 

implicit def neq[A, B] : A =:!= B = new =:!=[A, B] {}
implicit def neqAmbig1[A] : A =:!= A = ???
implicit def neqAmbig2[A] : A =:!= A = ???
like image 63
Miles Sabin Avatar answered Jan 22 '23 16:01

Miles Sabin