Function pointers/delegates default args were stealth removed?

Artur Skawina art.08.09 at gmail.com
Mon Aug 27 03:34:21 PDT 2012


On 08/27/12 10:24, Walter Bright wrote:
> On 8/27/2012 1:10 AM, Manu wrote:
>> I don't see how the linker enters into it. Default args are irrelevant to the
>> linker.
> 
> Consider name mangling and what it's for. Now consider two *different* types mangling to the same name.
> 
> D fundamentally depends on a 1:1 correspondence between types and name mangling, not 1:n or n:1.

D or DMD? Because the relevant bug /was/ an implementation problem. I can see how
changing the implementation is not really practical short-term, hence didn't comment
on it at the time, but even the examples given as "unsound" were fine and could be
well defined. Type equivalence != type identity. Default args shouldn't be part
of the mangled names. But having them (defargs) for function pointers can be very
useful, esp. for generics ("void f(int=42); auto fp = &f; fp();" could be made to
work), it reduces the amount of glue required and increases productivity - one of
D's main strengths.

In the mean time hacks such as this one can probably help sometimes, like
in the automatically-generated-bindings cases:

   void f(int one, string two, double three) {
      import std.stdio;
      writeln(one, two, three);
   }

   void main() {
      auto fp = CWDA!(typeof(&f), "1", "\"two\"", "3.14")(&f);
      fp(1, "two", 3.14);
      fp(1, "two");
      fp(1);
      fp();
      fp(four/4);
   }

   int four = 4;

   static struct _CWDA(C, DA...) {
      C ptr;
      alias ptr this;

      import std.traits;

      static if (DA.length==3) // POC; handling other cases left as an exercise ;)
      auto ref opCall(ParameterTypeTuple!C[0..$-3] a,
                      ParameterTypeTuple!C[$-3] da1 = mixin(DA[$-3]),
                      ParameterTypeTuple!C[$-2] da2 = mixin(DA[$-2]),
                      ParameterTypeTuple!C[$-1] da3 = mixin(DA[$-1])) {
         return ptr(a, da1, da2, da3);
      }
   }

   auto CWDA(C, DA...)(C c) { _CWDA!(C, DA) p; p.ptr = c; return p; }

but that's not really an acceptable solution, obviously.

artur


More information about the Digitalmars-d mailing list