实施 Summable 时遇到问题

Trouble implementing Summable

我正在尝试实现 类 DistanceCMDistanceMM,我希望它们可以互换求和,只要它们都继承自 Distance

但是,我得到这个错误:

"Error:(46, 76) ceylon: type parameter 'Other' of declaration 'Summable' has argument 'Distance' which is not assignable to upper bound 'Summable' of 'Other'"

...我无法破译...错误消息指的是下面代码中的这一行:

shared actual Distance plus(Distance other) 

这是当前代码:

abstract class Distance() of DistanceMM | DistanceCM {
    shared formal Distance multiplyScalar(Float scalar);
}


class DistanceMM(variable Float val) extends Distance() satisfies Summable<Distance>
{
    shared Float distanceInMillimeters;
    shared Float distanceInCentimeters;

    switch (unit)
    case (millimeter) {
        distanceInMillimeters => val;
        distanceInCentimeters => val / 10;
    }
    case (centimeter) {
        distanceInMillimeters => val * 10;
        distanceInCentimeters => val;
    }


    shared actual DistanceMM multiplyScalar(Float scalar) {
        val = val * scalar;
        return this;
    }


    shared actual Distance plus(Distance other) {
            switch (other)
            case (DistanceMM) {
                return DistanceMM(val + other.distanceInMillimeters(), unit);
            }
            case (DistanceCM) {
                return DistanceMM(val + other.distanceInCentimeters(), unit);
            }
    }
}


class DistanceCM(variable Float val) extends Distance() satisfies Summable<Distance>
{
    shared Float distanceInMillimeters;
    shared Float distanceInCentimeters;

    switch (unit)
    case (millimeter) {
        distanceInMillimeters => val;
        distanceInCentimeters => val / 10;
    }
    case (centimeter) {
        distanceInMillimeters => val * 10;
        distanceInCentimeters => val;
    }

    shared actual DistanceCM multiplyScalar(Float scalar) {
        val = val * scalar;
        return this;
    }
    // implementation missing
}
interface Summable<Other> of Other​ given Other satisfies Summable<Other>

注意约束(given 子句)。您声称 DistanceMM 满足 Summable<Distance>,但 Distance 不满足 Other 的约束(Distance 不满足 Summable<Distance> ).试试这个:

interface Distance of Centimeter | Millimeter satisfies Summable<Distance> {}

class Centimeter() satisfies Distance {
    shared actual Distance plus(Distance other) => nothing;
}

class Millimeter() satisfies Distance {
    shared actual Distance plus(Distance other) => nothing;
}