Pretty please: Named arguments

Andrej Mitrovic andrej.mitrovich at gmail.com
Mon Feb 28 12:12:01 PST 2011


And maybe sometimes you just want to update one field, e.g.:

void resize(int width=0, int height=0)
{
    if (width)
        // set new width
    if (height)
        // set new height
}

void main()
{
    resize(width:20, height:40);
    // more code..

    resize(width:50);  // only update width
}

Using a struct could potentially introduce bugs, e.g.:
void resize(Size size) { // this.size = size.. }
void main()
{
    Size size;
    size.width = 20;
    size.height = 40;
    resize(size);

    // more code
    size.width = 50;
    resize(size);
}

What if between the two calls to resize some other part of code has
manipulated the height? The size struct still has its height member
set to 40. So (for example) your window might suddenly jump in height
even though the user has updated only the width field.

Now you have to do extra housekeeping to keep things in order (e.g.
resetting the height field of the struct so the call to resize doesn't
touch the window height).


More information about the Digitalmars-d mailing list