On Friday, 23 October 2015 at 14:58:43 UTC, pineapple wrote:
Is it possible to have default method implementations in interfaces à la Java in D? Or some equivalent that allows multiple inheritance without a bunch of identical copypasted method bodies?

Use template mixins: http://dlang.org/template-mixin.html

interface MyInterface {
    void foo();
    int bar();
}

mixin template MyInterfaceDefaultImpl() {
    void foo() {
        // put code here
    }
    int bar() {
        // put code here
    }
}

class MyClass : MyInterface {
mixin MyInterfaceDefaultImpl!(); // Similar to inserting the body of `MyInterfaceDefaultImpl` at this point. mixin MyOtherInterfaceDefaultImpl!(); // Can put any amount of them here.
}

Reply via email to