Skip to content

Instantly share code, notes, and snippets.

@purijatin
Last active September 23, 2016 06:51
Show Gist options
  • Save purijatin/53ffaa8c1d485ed13b07256340099d51 to your computer and use it in GitHub Desktop.
Save purijatin/53ffaa8c1d485ed13b07256340099d51 to your computer and use it in GitHub Desktop.
New.scala
To write a method which takes two type parameters and both should not be the same:
trait =!=[A,B]
implicit def neq[A,B]: =!=[A,B] = null
implicit def eqAmb[A]: =!=[A,A] = null
implicit def eqAmbig2[A]: =!=[A,A] = null
case class Foo2[A,B]( a: A, b: B )( implicit ev: A =!= B )
//Foo2(1,2) compile error
case class Foo3[A,B]( a: A, b: B )( implicit ev: A =!= B, ev2: String =!= B, ev3: A =!= String )
//above is for none of the type parameter to be String. You need to do at both the places
def notString[T](t : T)(implicit ev: T =!= String) = t
http://stackoverflow.com/questions/6909053/enforce-type-difference/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment