I really like Scala for its flexibility and conciseness. With this definition of a money amount:
case class MoneyAmount(amount: Double, currency: Currency)
trait Currency
case object EUR extends Currency
case object USD extends Currency
You can create instances easily:
val m1 = MoneyAmount(100, EUR)
With the following definition in scope its even easier:
implicit class DoubleMoney(amount: Double) {
def apply(currency: Currency) = MoneyAmount(amount, currency)
}
val m2 = 100 (EUR)
val m3 = 100 (USD)
My question is: Is there a way that the following is possible:
val m3 = 100 EUR // does not compile!
without defining functions for each currency (EUR, USD, ...)?