Last active
February 12, 2019 04:08
-
-
Save manishkkatoch/f395f70fc2ab5e10ed072db27fe3c073 to your computer and use it in GitHub Desktop.
Discounted.scala
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
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