www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Request: traits isImplemented

reply "JS" <js.mdnq gmail.com> writes:
I have a request to add to __traits to check if a method/property 
of a class/struct is actually implemented or not.

interface A { void myfunc(); }

class B : A { } // myfunc not implemented
class C : A { void myfunc() { ... } // myfunc implemented

This may seem odd but I need it for conditional mixin compilation 
to add in the methods when they are not implemented. A sort of 
default proxy, wrapper, or composite pattern implementer. It 
reduces boilerplate code significantly.

Unfortunately allMembers returns methods that are not actually 
implemented or part of the class.

Something like allImmediateMembers would be useful where it only 
returns the actually explicit members of the immediate 
class/struct that it is used on and accomplish the same task.
Jul 05 2013
next sibling parent reply Jacob Carlborg <doob me.com> writes:
On 2013-07-05 11:52, JS wrote:
 I have a request to add to __traits to check if a method/property of a
 class/struct is actually implemented or not.

 interface A { void myfunc(); }

 class B : A { } // myfunc not implemented
 class C : A { void myfunc() { ... } // myfunc implemented

 This may seem odd but I need it for conditional mixin compilation to add
 in the methods when they are not implemented. A sort of default proxy,
 wrapper, or composite pattern implementer. It reduces boilerplate code
 significantly.

 Unfortunately allMembers returns methods that are not actually
 implemented or part of the class.

 Something like allImmediateMembers would be useful where it only returns
 the actually explicit members of the immediate class/struct that it is
 used on and accomplish the same task.

derivedMembers perhaps: http://dlang.org/traits.html#derivedMembers -- /Jacob Carlborg
Jul 05 2013
parent Jacob Carlborg <doob me.com> writes:
On 2013-07-05 19:44, JS wrote:

 Thanks for being so helpful... I bet you know exactly where it is at and
 that it does exactly what I need! You're so smart!

http://dlang.org/traits.html#getOverloads -- /Jacob Carlborg
Jul 06 2013
prev sibling next sibling parent "JS" <js.mdnq gmail.com> writes:
On Friday, 5 July 2013 at 10:55:26 UTC, Jacob Carlborg wrote:
 On 2013-07-05 11:52, JS wrote:
 I have a request to add to __traits to check if a 
 method/property of a
 class/struct is actually implemented or not.

 interface A { void myfunc(); }

 class B : A { } // myfunc not implemented
 class C : A { void myfunc() { ... } // myfunc implemented

 This may seem odd but I need it for conditional mixin 
 compilation to add
 in the methods when they are not implemented. A sort of 
 default proxy,
 wrapper, or composite pattern implementer. It reduces 
 boilerplate code
 significantly.

 Unfortunately allMembers returns methods that are not actually
 implemented or part of the class.

 Something like allImmediateMembers would be useful where it 
 only returns
 the actually explicit members of the immediate class/struct 
 that it is
 used on and accomplish the same task.

derivedMembers perhaps: http://dlang.org/traits.html#derivedMembers

This doesn't work with overloaded members. It's possible that a function with the same name may be completely unrelated to the interface being implemented.
Jul 05 2013
prev sibling next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 7/5/13, JS <js.mdnq gmail.com> wrote:
 I have a request to add to __traits to check if a method/property
 of a class/struct is actually implemented or not.

 interface A { void myfunc(); }

 class B : A { } // myfunc not implemented
 class C : A { void myfunc() { ... } // myfunc implemented

 This may seem odd but I need it for conditional mixin compilation
 to add in the methods when they are not implemented.

Perhaps looking at the BlackHole or WhiteHole implementation can help you out to implement this via a template? They're in std.typecons.
Jul 05 2013
prev sibling next sibling parent "Jacob Carlborg" <doob me.com> writes:
On Friday, 5 July 2013 at 14:59:19 UTC, JS wrote:

 This doesn't work with overloaded members. It's possible that a 
 function with the same name may be completely unrelated to the 
 interface being implemented.

There's a trait for that as well. Read the documentation. -- /Jacob Carlborg
Jul 05 2013
prev sibling next sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 07/05/2013 02:52 AM, JS wrote:
 I have a request to add to __traits to check if a method/property of a
 class/struct is actually implemented or not.

 interface A { void myfunc(); }

 class B : A { } // myfunc not implemented
 class C : A { void myfunc() { ... } // myfunc implemented

 This may seem odd but I need it for conditional mixin compilation to add
 in the methods when they are not implemented. A sort of default proxy,
 wrapper, or composite pattern implementer. It reduces boilerplate code
 significantly.

 Unfortunately allMembers returns methods that are not actually
 implemented or part of the class.

 Something like allImmediateMembers would be useful where it only returns
 the actually explicit members of the immediate class/struct that it is
 used on and accomplish the same task.

Can you make A a class with default myfunc() implementation? class A { void myfunc() { writeln("default myfunc"); } } class B : A { } class C : A { override void myfunc() { writeln("C.myfunc"); } } Or, you can leave A as an interface and insert another class after it that has the default implementation: A | ADefaults | | B C Ali
Jul 05 2013
prev sibling parent "JS" <js.mdnq gmail.com> writes:
On Friday, 5 July 2013 at 16:04:19 UTC, Jacob Carlborg wrote:
 On Friday, 5 July 2013 at 14:59:19 UTC, JS wrote:

 This doesn't work with overloaded members. It's possible that 
 a function with the same name may be completely unrelated to 
 the interface being implemented.

There's a trait for that as well. Read the documentation. -- /Jacob Carlborg

Thanks for being so helpful... I bet you know exactly where it is at and that it does exactly what I need! You're so smart!
Jul 05 2013