Skip to content

Instantly share code, notes, and snippets.

@afsalthaj
Last active March 18, 2018 12:22
Show Gist options
  • Save afsalthaj/3903d515a224d1dc28e6a10227ee0f8a to your computer and use it in GitHub Desktop.
Save afsalthaj/3903d515a224d1dc28e6a10227ee0f8a to your computer and use it in GitHub Desktop.
sealed trait Fruits
object Fruits {
case class Banana() extends Fruits
case class Apple() extends Fruits
}
// This could be just FruitsBehaviou[-A]
trait FruitsBehavior[-A <: Fruits] {
def price(a: A): Int
}
object FruitsBehavior {
import Fruits._
def price[A <: Fruits : FruitsBehavior](a: A): Int =
implicitly[FruitsBehavior[A]].price(a)
implicit object BananaInstance extends FruitsBehavior[Banana] {
override def price(a: Banana): Int = 0
}
implicit object AppleInstance extends FruitsBehavior[Apple] {
override def price(a: Apple): Int = 1
}
}
object ThePlaceWhereIUse {
import Fruits._
def main(args: Array[String]): Unit =
println(FruitsBehavior.price(Apple()))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment