Skip to content

Instantly share code, notes, and snippets.

@mrcmatuszak
Created April 27, 2016 21:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrcmatuszak/e6bdcfe5ed70c62f549ea4dbd6a7d520 to your computer and use it in GitHub Desktop.
Save mrcmatuszak/e6bdcfe5ed70c62f549ea4dbd6a7d520 to your computer and use it in GitHub Desktop.
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