Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
July 05, 2013 Request: traits isImplemented | ||||
---|---|---|---|---|
| ||||
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. |
July 05, 2013 Re: Request: traits isImplemented | ||||
---|---|---|---|---|
| ||||
Posted in reply to JS | 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 |
July 05, 2013 Re: Request: traits isImplemented | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | 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.
|
July 05, 2013 Re: Request: traits isImplemented | ||||
---|---|---|---|---|
| ||||
Posted in reply to JS | 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.
|
July 05, 2013 Re: Request: traits isImplemented | ||||
---|---|---|---|---|
| ||||
Posted in reply to JS | 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
|
July 05, 2013 Re: Request: traits isImplemented | ||||
---|---|---|---|---|
| ||||
Posted in reply to JS | 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
|
July 05, 2013 Re: Request: traits isImplemented | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | 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!
|
July 06, 2013 Re: Request: traits isImplemented | ||||
---|---|---|---|---|
| ||||
Posted in reply to JS | 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 |
Copyright © 1999-2021 by the D Language Foundation