나는 경로 의존형을 실험하고있다. 내 간단한 예제에서는 Money 개체를 사용하여 동일한 통화의 Money에서만 Money 계산을 수행 할 수 있습니다.런타임에 경로 종속 유형과 해당 부모 간의 링크를 다시 설정 하시겠습니까?
// Simple currency class
case class Currency(code: String, name: String, symbol: String) {
// Money amounts in this currency
// Operations are only supported on money of the same currency
case class Money(amount: BigDecimal) {
override def toString: String = s"$code $amount"
val currency: Currency.this.type = Currency.this
def +(rhs: Money): Money = Money(amount + rhs.amount)
def -(rhs: Money): Money = Money(amount - rhs.amount)
}
}
위의 클래스를 사용하면 repl의 간단한 계산은 straigh forward입니다.
val e1 = Euro.Money(5)
val e2 = Euro.Money(9)
e1 + e2 // Compiles fine
val d1 = Dollar.Money(6)
d1 + e2 // Doesn't compile as expected
컴파일러가 e1과 e2가 공통 통화를 공유하고 있음을 쉽게 증명할 수 있기 때문에 이는 간단합니다. 그러나 파일이나 데이터베이스에서 금액 금액을 읽을 때 돈 인스턴스가 공통 통화를 공유한다는 사실을 증명하는 것이 훨씬 어렵습니다. 예를 들어 아래의 콜레이트 함수를 구현하는 방법을 볼 수 없습니다.
trait CurrencyAndMonies {
val currency: Currency
val monies: List[currency.Money]
}
// Take a list of money in different currencies and group them by currency
// so their shared Currency type is available to static type checking
// in further calculations
def collate(Seq[Currency#Money]): List[CurrencyAndMonies] = ???
통화를 기준으로 돈을 정렬하고 이들 사이의 링크를 재설정 할 수 있습니까? 그리고 만약 그렇다면 어떻게? 서명이나 돈 금액을 데이터베이스에서 읽는 방법을 변경하는 데는 신경 쓰지 않아도됩니다. 당신은 낙심 할 수
답해 주셔서 감사합니다. 나는 당신이 옳다 고 생각합니다. 나는 그것을 던져야 할 것입니다. 그것은 동정입니다. 나는 그 순간에 그것을 캐스팅하는 깔끔한 방법을보고있다. (어쩌면 안전을 위해 패턴 매칭 뒤에 놓는다.) – iain