[Feature Request] Forwardable as Ruby does
kennytm
kennytm at gmail.com
Mon Mar 5 23:12:00 PST 2012
bioinfornatics <bioinfornatics at fedoraproject.org> wrote:
> Le lundi 05 mars 2012 ‡ 17:55 +0000, kennytm a Ècrit :
>> bioinfornatics <bioinfornatics at fedoraproject.org> wrote:
>>> dear,
>>> I would like a easy way to forward as does ruby:
>>> http://www.ruby-doc.org/stdlib-1.9.3/libdoc/forwardable/rdoc/Forwardable.html
>>>
>>> I have already something close:
>>> - https://gist.github.com/1969776 (me)
>>> - http://paste.pocoo.org/show/uoD89CCjX5H3m6GvPvW6/ (dav1d)
>>>
>>> But it works only for array.
>>>
>>>
>>> I have try to do this purpose: https://gist.github.com/1974033
>>>
>>> but i am too new in D
>>>
>>> This feature will be really awesome and could be go into std.traits
>>
>> Check 'opDispatch' and 'alias this'.
>
> alias this is not the answer as it forward all, i fyo want select for
> this member forwad this method and for another this one ...
>
> opDispaytch is not really easy to use for this. my first code do this in
> 1 line and it is easy to understand hat it do. In 1 line i forward 3
> method to a member. This save time, code and bug.
You can just mixin an opDispatch function, like (not tested, only works for
functions)
mixin template Forward(alias member, s...) {
auto opDispatch(string name, T...)(T args) {
static assert(canFind(s, name));
return mixin("member." ~ name ~ "(args)");
}
}
or use a foreach over 's' and mixin the function/member definitions for
each name.
More information about the Digitalmars-d
mailing list