Evidence that types are not equal in Scala

Is there a way to limit the method so that it makes sense only if the two types are not 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)
}
+4
source share
1 answer

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 = ???
+10
source

Source: https://habr.com/ru/post/1546750/


All Articles