this is almost a workaround for the lack of named parameters

J not_avail at notavailable.com
Thu Mar 21 12:42:07 PDT 2013


   /*  Similarly but different (here I am instantiating a new 
struct
       before the call, rather than re-using a global single 
struct each time),
       it would be lovely and elegant to say:
   */

void main(string[] arg) {

   /* Elegant:

      auto r = myfunc() with { z= 2; x = -123; y = 200; }

   */

   // and have it lowered to:
   myfunc tmp;
   with(tmp) { z= 2; x = -123; y = 200; }
   auto r = tmp();

}

import std.stdio;

struct myfunc
{
    // named keyword or named parameters
    // --the call arguments and their defaults
    int x=0;
    int y=0;
    int z=0;

   // opCall is sweet... improves upon previous example

    string opCall(string required_positional_arg = "default pos 
arg value") {
      writefln("%s:  X %s, Y %s, Z %s", required_positional_arg, 
x, y, z );
      return "yo";
    }

}


More information about the Digitalmars-d mailing list