Pretty please: Named arguments

Joel C. Salomon joelcsalomon at gmail.com
Sun Mar 6 16:57:17 PST 2011


(De-lurking; I've been interested in D for a while, but my programming
is almost exclusively in C -- generally C99.)

Does D have the equivalent of C99's "designated initializers"?

Were I to attempt something like this in C, the code would go something
like this:

    int foo(int height, int width);
    struct _foo_args {
        int height; int width;
    };
    #define foo(...) \
        foo((struct _foo_args){__VA_ARGS__}.height, \
            (struct _foo_args){__VA_ARGS__}.width)

at which point each of these calls:

    foo(a, b);
    foo(.height = a, .width = b);
    foo(.width = b, .height = a);

have the same effect.

I'll readily admit this is *not* pretty, and likely more effort than
it's worth, but is will work.

Are D's compile-time operations not capable of something at *least* this
powerful?

--Joel


More information about the Digitalmars-d mailing list