The most awesome "forward to member" solution?
Meta via Digitalmars-d
digitalmars-d at puremagic.com
Sat May 2 22:00:16 PDT 2015
On Sunday, 3 May 2015 at 04:20:46 UTC, Andrei Alexandrescu wrote:
> On 5/2/15 5:42 PM, Meta wrote:
>> On Sunday, 3 May 2015 at 00:25:13 UTC, Dicebot wrote:
> Here's what I have right now - simple as they come:
> http://dpaste.dzfl.pl/7ec11459a125. Kudos to whoever defined
> ParameterTypeTuple, it's exactly what the doctor prescribed.
>
> The gist of it is:
>
> string forwardToMember(string member, string[] funs...)
> {
> string result;
> foreach (fun; funs)
> {
> result ~= "
> auto
> "~fun~"(ParameterTypeTuple!(typeof("~member~"."~fun~")) args)
> {
> return "~member~"."~fun~"(args);
> }
> ";
> }
> return result;
> }
>
> which is used as:
>
> mixin(forwardToMember("member", "expand", "reallocate",
> "owns"));
>
> Is this a common thing people wanna do? Put in Phobos?
>
>
> Andrei
Is there any particular reason to generate 1 function per parent
function? I was actually surprised the following doesn't work:
struct Test1
{
void foo(string) { writeln("Test1 foo"); }
int bar(int) { writeln("Test1 bar"); return 0; }
void baz() { writeln("Test1 baz"); }
}
struct TestWrapper(T)
{
T parent;
alias foo = parent.foo;
alias bar = parent.bar;
alias baz = parent.baz;
}
void main()
{
TestWrapper!Test1 t;
//Error: this for foo needs to be type Test1 not type
TestWrapper!(Test1)
t.foo("test");
//ditto
t.bar(2);
//ditto
t.baz();
}
It seems like it'd be a lot cheaper and cleaner to just be able
to alias the parent method. Also, it could probably be made a bit
simpler with opDispatch.
More information about the Digitalmars-d
mailing list