5
Jak zdefiniować nadklasę eliminującą płytkę z tych dwóch prostych klas przedziałów?Scala Real Interval, Int Interval
class IntInterval(val from: Int, val to: Int) {
def mid: Double = (from+to)/2.0
def union(other: IntInterval) = IntInterval(from min other.from, to max other.to)
}
class DoubleInterval(val from: Double, val to: Double) {
def mid: Double = (from+to)/2.0
def union(other: DoubleInterval) = DoubleInterval(from min other.from, to max other.to)
}
że próbował
class Interval[T <: Number[T]] (val from: T, val to: T) {
def mid: Double = (from.doubleValue+to.doubleValue)/2.0
def union(other: IntInterval) = Interval(from min other.from, to max other.to)
}
ale min i maks nie zawrzeć w sposobie związek (ponieważ numer [T], nie min/max).
można zapewnić eleganckie superklasę która zajmuje się zarówno Unia mid i metod w schludny, kod-raz-i-tylko-raz boilerplate unikania sposób?
Mhhh. Zweryfikowałem to w 2.9.2 i nie miałem z tym problemów. Jaki jest twój komunikat o błędzie? (Czy zapomniałeś 'import num ... '?) – soc
dzięki bardzo wiele! Działało idealnie. –