On Tuesday, 5 December 2017 at 07:47:32 UTC, Dirk wrote:
The distance function is implementation dependend and can only be computed between two objects of the same class (in this example the class is Item).

Just don't put it in the interface. Leave it in the individual classes with the stricter definition.

Or, as I said before, implement the interface by asking the question: what if you call Item.distance (NotAnItem)? Maybe you could throw an exception or return NaN to show this is an invalid comparison. But if it is in the interface, you MUST answer that question somehow.

---
interface Medoid {
    float distance(Medoid other);
}

class Item {
    float distance(Medoid other) {
       if(auto item = cast(Item) other) {
            // it is an item, implement comparison here
       } else {
            return float.nan; // invalid comparison
       }
    }
}
---


Tip: you can generically test that two objects are of the same class through their interfaces by checking:

if(typeid(cast(Object) o1) == typeid(cast(Object) o2)) {
   // same class
} else {
   // same interface, but different class
}

Might be useful for your classifier, though checking the nan of distance might be good too.

Reply via email to