Last active
March 18, 2018 12:22
-
-
Save afsalthaj/3903d515a224d1dc28e6a10227ee0f8a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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