Skip to content

Instantly share code, notes, and snippets.

@manishkkatoch
Last active February 12, 2019 04:08
Show Gist options
  • Save manishkkatoch/f395f70fc2ab5e10ed072db27fe3c073 to your computer and use it in GitHub Desktop.
Save manishkkatoch/f395f70fc2ab5e10ed072db27fe3c073 to your computer and use it in GitHub Desktop.
Discounted.scala
trait Discounted[T] {
def getDiscount: Double
}
object Discounted {
// apply method called by compiler to instantiate if no instance found
def apply[T](implicit discounted: Discounted[T]) = discounted
def createDiscounted[T](fn: () => Double) = new Discounted[T] {
override def getDiscount: Double = fn()
}
//implicit instances of Discounted for each Membership Card
implicit val silverCardDiscounted: Discounted[Silver] = createDiscounted( () => 0.0)
implicit val goldCardDiscounted: Discounted[Gold] = createDiscounted(() => 10.0)
implicit val platinumCardDiscounted: Discounted[Platinum] = createDiscounted(() => 15.0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment