Created
October 7, 2012 21:55
-
-
Save michaeldiamant/3849746 to your computer and use it in GitHub Desktop.
Tagged types representing units of price
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 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