Objective-D, reflective programming, dynamic typing

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Fri Apr 3 22:07:26 PDT 2009


Christopher Wright wrote:
> Andrei Alexandrescu wrote:
>> Jarrett Billingsley wrote:
>>> On Fri, Apr 3, 2009 at 11:20 AM, Andrei Alexandrescu
>>> <SeeWebsiteForEmail at erdani.org> wrote:
>>>
>>>>> You can't.  D's varargs suck.
>>>> Of course you can. Where did that come from?
>>>>
>>>> void foo(T...)(T args) { bar(args); }
>>>> void bar(T...)(T args) {  foreach (a; args) writeln(a); }
>>>
>>> *NOT* the same thing.  They are templated, which means a new
>>> instantiation for each different combination of argument types, and
>>> cannot be virtual.  Please keep that in mind.
>>
>> But my point was that variadic templates can be ideal wrappers for the 
>> non-templated variadics, which can be virtual. It's the best of both 
>> worlds, you get no bloating and comfortable calling syntax too.
>>
>> I think it's silly to ask "I want to do... xyz... but without 
>> templates because templates aren't virtual/cause bloating/bring up 
>> painful memories". Templates are an excellent starting point in many 
>> designs because they preserve full typing of the parameters. Then, of 
>> course you can route them to dynamic functions no problem.
> 
> Okay, so before I had to write:
> 
> void foo(...)
> {
>    foo(_argptr, _arguments);
> }
> void foo(void* argptr, TypeInfo[] argtypes) {}
> 
> 
> You want me to instead write:
> 
> // needs a different name due to overload issues
> void foo_templated(T...)(T args)
> {
>    foo(args);
> }
> void foo(...)
> {
>    foo(_argptr, _arguments);
> }
> void foo(void* argptr, TypeInfo[] argtypes) {}
> 
> How the hell does that help?

void foo_templated(T...)(T args)
{
    Variant[args.length] dynatyped;
    foreach (i, arg; args) dynatyped[i] = arg;
    return foo(dynatyped);
}
void foo(Variant[] dynatyped...)
{
    ...
}

Andrei



More information about the Digitalmars-d mailing list