why can't structs implement interfaces?

bearophile bearophileHUGS at lycos.com
Tue Nov 24 21:07:26 PST 2009


Daniel Keep:

> This is why interfaces cannot be implemented by structs in D: it would
> require structs to grow magical hidden fields, which is explicitly
> against the stated purpose of structs: plain old data.

In D2 if you define a struct inside a function it (in theory) grows an hidden field, see Nested Structs here:
http://www.digitalmars.com/d/2.0/struct.html

I have written in theory because in practice the size is the same :-) And the following code doesn't compile, static structs are not implemented yet it seems:

void foo() {
    static struct Inner {
        int x;
    }
}

Walter needs an automatic way to test code snippets inside the docs...

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list