Created
April 27, 2016 21:41
-
-
Save mrcmatuszak/e6bdcfe5ed70c62f549ea4dbd6a7d520 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
package test | |
import shapeless.{HNil, Poly1} | |
object Main extends App { | |
sealed trait Product { | |
def name: String | |
def unitPrice: BigDecimal | |
} | |
case class Phone(name: String, unitPrice: BigDecimal) extends Product | |
case class Accessory(name: String, unitPrice: BigDecimal) extends Product | |
trait Pricer[T] { | |
def calculatePrice(t: T): BigDecimal | |
} | |
object StandardPrices extends Poly1 { | |
implicit def phonePricer = at[Phone] { | |
new Pricer[Phone] { | |
def calculatePrice(p: Phone): BigDecimal = p.unitPrice | |
}.calculatePrice _ | |
} | |
implicit def accessoryPricer = at[Accessory] { | |
new Pricer[Accessory] { | |
def calculatePrice(p: Accessory): BigDecimal = p.unitPrice | |
}.calculatePrice _ | |
} | |
} | |
val card = Phone("Nexus 5", 100.0) :: Accessory("Qi charger", 30) :: HNil | |
println(card.map(StandardPrices)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment