Created
December 14, 2011 23:46
-
-
Save retronym/1479149 to your computer and use it in GitHub Desktop.
Currency
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
// 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