Skip to content

Instantly share code, notes, and snippets.

@michaeldiamant
Created October 7, 2012 21:55
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 michaeldiamant/3849746 to your computer and use it in GitHub Desktop.
Save michaeldiamant/3849746 to your computer and use it in GitHub Desktop.
Tagged types representing units of price
trait Usd
trait Eur
trait ProfitCalculator[T] {
def calculateProfit(executionPrice: BigDecimal @@ T, requestedPrice: BigDecimal @@ T): BigDecimal @@ T =
tag(executionPrice - requestedPrice)
}
object ProfitCalculatorExample extends App {
val usdProfitCalculator = new ProfitCalculator[Usd] {}
// Does not compile
// usdProfitCalculator.calculateProfit(
// executionPrice = tag[Eur](BigDecimal(5)),
// requestedPrice = tag[Usd](BigDecimal(5))
// )
val profit = usdProfitCalculator.calculateProfit(
executionPrice = tag[Usd](BigDecimal(5)),
requestedPrice = tag[Usd](BigDecimal(5))
)
println(profit)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment