How to determine if a function is implemented

Kenji Hara k.hara.pg at gmail.com
Thu Jul 4 23:34:57 PDT 2013


On Friday, 5 July 2013 at 01:27:06 UTC, JS wrote:
> the code
>
> http://dpaste.dzfl.pl/25bfeeb7
>
> attempts to implement an interface. The current issue is that I 
> need to determine if the user has added the member of the 
> interface to the class or if the mixin needs to add it.
>
>
> so the lines
>
> class B : A
> {
> 	A a;
> 	
> 	//void myfunc(float a, int b, string c) { };
> 	//@property int myvalue() { return 4; }
> 	mixin implementInterface!a;
> }
>
> The mixin adds the two commented functions above it which 
> effectively implement the interface A in B. The problem is, I 
> might want to manually specify one, e.g.,
>
>
> class B : A
> {
> 	A a;
> 	
> 	void myfunc(float a, int b, string c) { };
> 	//@property int myvalue() { return 4; }
> 	mixin implementInterface!a;
> }
>
> So the mixin needs to be aware and not add a method that is 
> already implemented.
> I need some way for the mixin to distinguish the two cases 
> above. e.g., isImplemented!(myfunc(float, int, string)) or 
> something like that.

It's completely unnecessary. A mixed-in function cannot override 
properly declared function that has same name in the mixed-in 
scope.


interface I { int foo(); }

mixin template Foo()
{
     override int foo() { return 1; }
}

class C1 : I {
     mixin Foo!();
}

class C2 : I
{
     int foo() { return 10; }
     mixin Foo!();
     // mixed-in foo is not stored in vtbl
}

void main()
{
     assert(new C1().foo() == 1);
     assert(new C2().foo() == 10);
}

Kenji Hara


More information about the Digitalmars-d-learn mailing list