interface function member declarations needing parameter attributes ?

Adam D Ruppe destructionator at gmail.com
Sun Jul 18 11:03:24 UTC 2021


On Sunday, 18 July 2021 at 03:27:04 UTC, someone wrote:
> I have an interface with function members *not* being declared 
> @safe (at first glance it seemed irrelevant to me just to mark 
> @safe a declaration and not an actual implementation).

Yeah, that'd be promising all child implementations are @safe.

Fun fact there: any child implementations do NOT need to specify 
the attribute there; the compiler will copy it from the interface 
for you.

interface I {
    @safe void foo();
}

class C : I {
    void foo(); // automatically copies @safe from interface
}


If you leave it off, you are not promising safe, but children are 
still allowed to use it anyway. The general rule is child classes 
can be stricter than the parent if they want to be, but they 
don't have to be.


interface I {
    void foo(); // not safe
}

class C : I {
     void foo(); // OK, not safe, interface didn't force it
}

class C2 : I {
     @safe void foo(); // OK, class can be stricter than parent. 
now safe if used through C2, but the interface can't promise it 
is C2 instead of C, so it still follows @system rules.
}

class C3 : C2 {
     override void foo(); // automatically @safe since it picks it 
up from parent C2
}


More information about the Digitalmars-d-learn mailing list