Skip to content

Instantly share code, notes, and snippets.

@amitprasad119
Created October 5, 2021 09:37
Show Gist options
  • Save amitprasad119/cfac1a84aab7e7c563fc3ab55b4e6454 to your computer and use it in GitHub Desktop.
Save amitprasad119/cfac1a84aab7e7c563fc3ab55b4e6454 to your computer and use it in GitHub Desktop.
object TypeClassesExample extends App {
case class User(name: String, age: Int, city: String)
trait Equality[T] {
def isEqual(left: T, right: T): Boolean
}
object Equality {
def isEqual[T](left: T, right: T)(implicit equality: Equality[T]) =
equality.isEqual(left, right)
}
implicit object IntEquality extends Equality[Int] {
override def isEqual(left: Int, right: Int): Boolean = left == right
}
implicit object StringEquality extends Equality[String] {
override def isEqual(left: String, right: String): Boolean = left.equalsIgnoreCase(right)
}
implicit object UserEquality extends Equality[User] {
override def isEqual(left: User, right: User): Boolean =
left.age == right.age // just override the equal rule on age
}
println(Equality.isEqual(1, 2))
println(Equality.isEqual(User("Peter", 29, "London"), User("John", 29, "Bangalore")))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment