Object oriented programming and interfaces

bauss jj_1337 at live.dk
Tue Dec 5 10:44:39 UTC 2017


On Tuesday, 5 December 2017 at 08:08:55 UTC, Daniel Kozak wrote:
> You can do something like this:
>
> interface Medoid(T) {
>     float distance( T other );
>     uint id() const @property;
> }
>
> class Item : Medoid!(Item) {
>     float distance( Item m ) { return 0.;}
>     uint id() const @property { return 1; }
> }
>
> class MedoidClassification {
>     this(T:Medoid!T)(T[] list) {}
>     //Medoid[][] getClusters() {...}
> }
>
> void main() {
>     auto items = new Item[10];
>     auto mc = new MedoidClassification( items );
> }
>
> On Tue, Dec 5, 2017 at 8:47 AM, Dirk via Digitalmars-d-learn < 
> digitalmars-d-learn at puremagic.com> 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).
>>
>> My goal is to write a module for a k-medoids clustering 
>> algorithm. The class MedoidClassification shall be able to 
>> partition a list of objects from the same class, which 
>> implement the Medoid interface.
>>
>> My current approach is this (which does not work):
>>
>> interface Medoid {
>>     float distance( Medoid other );
>>     uint id() const @property;
>> }
>>
>> class Item : Medoid {
>>     float distance( Item m ) {...}
>>     uint id() const @property {...}
>> }
>>
>> class MedoidClassification {
>>     this( Medoid[] list ) {...}
>>     Medoid[][] getClusters() {...}
>> }
>>
>> void main() {
>>     Item[10] items;
>>     auto mc = MedoidClassification( items );
>> }
>>
>>
>> What would be a good way to implement this?

This still defeats the purpose of having multiple Medoid types, 
as each Medoid is still specified with a specific type.


More information about the Digitalmars-d-learn mailing list