How to enter function by-name argument in D?

I am trying to pass arguments by the name of a function that has default parameter values:

import std.stdio;

void main() {

    void foo(int x=1, int y=2, int z=3) {
        writefln("x=%s, y=%s, z=%s", x, y, z);
    }

    foo(10, 20, 30); // ok, prints: x=10, y=20, z=30

    foo(z=30); // Error: undefined identifier 'z'
}

This is a fairly simple need. Such a function can have 10 parameters or more and can be called several times with a different set of arguments. It would be unbearable to list all the arguments every time - this will require an accurate knowledge of the positions, which may even change in new versions of my application. Or a new version of my application may change the default value. I will need to go through all the source code to change it.

I think the main thing should be in such a comprehensive language as D.

+4
source share
1 answer

D . , , - ...

, , " " . , , . Python - . , 20 , , ...

10 , , , .

, . #: http://www.dotnetperls.com/named-parameters.

+7

Source: https://habr.com/ru/post/1614849/


All Articles