Skip to content

Instantly share code, notes, and snippets.

@retronym
Created December 14, 2011 23:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save retronym/1479149 to your computer and use it in GitHub Desktop.
Save retronym/1479149 to your computer and use it in GitHub Desktop.
Currency
// Compile with Scala 2.10+ or 2.9.1 with -Ydependent-method-types
class Ccy {
trait AbstractCurrency{
type Currency <: AbstractCurrency
def value : Double
def make(d:Double) : Currency
def +(x: Currency): Currency = make(x.value + value)
}
class USD(val value: Double) extends AbstractCurrency {
type Currency = USD
def make(d: Double) = new USD(d)
}
class EUR(val value: Double) extends AbstractCurrency {
type Currency = EUR
def make(d: Double) = new EUR(d)
}
def plus[T <: AbstractCurrency](c1: T)(c2: c1.Currency) : c1.Currency = c1 + c2
val x : USD = plus(new USD(100))(new USD(200))
new EUR(100) + new EUR(50)
//plus(new USD(100))(new EUR(200))
//new EUR(100) + new USD(50)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment