On Monday, 25 March 2013 at 18:11:54 UTC, J wrote:
On Monday, 25 March 2013 at 16:03:38 UTC, Jacob Carlborg wrote:
On 2013-03-21 19:35, J wrote:

[snip]

I just noticed that Michel Fortin has implemented basic support for named parameters. I've updated his code to the latest DMD and pushed it to my fork:

https://github.com/jacob-carlborg/dmd/commit/e363a093040c4b14e7e814027c3e199676c82820

From the commit message:

Add basic support for named parameters.

This will only allow to add the name of the parameter
when calling a function. It doesn't not support passing
the arguments out of order.

All credit goes to Michel Fortin.

+1000

Excellent!

Wow. This is really nice! I added a method to Michel's nice work. You can get it in this branch.

https://github.com/glycerine/dmd/tree/namedarg

All the dmd test suite then passes.

You can now do this:

void test (double y, double z, int a, int b, int c, string d) { }

void main ()
{
    test(y: 33.3,
         z: 44.4,
         a: 9999,
         b: 8888,
            7777,   // truly optional
         d:"Yehaw");
}


I also tested the passing of named parameters through variadic templates, and there is a little work needed to make them 'named argument' aware. I added a failing test for that, if anyone wants to tackle it. It is dmd/test/compilable/namedarg_vararg.d in the branch above.

$ cat namedarg_vararg.d
cat namedarg_vararg.d
import core.vararg;
import std.stdio;

void test(A...)(A a)
{
    foreach(t; a)
        writeln(t);
}

void main ()
{
    test(x:33.3,
         z: 44.4,
         a: 9999,
         b: 8888,
         7777,
         d:"Yehaw");
}

$ dmd namedarg_vararg.d
namedarg_vararg.d(12): Error: template namedarg_vararg.test(A...)(A a) cannot deduce template function from argument types !()(x:double,z:double,a:int,b:int,int,d:string)
$

 * * *

Wierd and cool: Since the current version requires the exact same positions as names, it is almost like you get extra refined type safety for free. Say I have a function that takes two strings. The compiler can now help me if I mistakenly switch the two...

Example:

void find(string needle, string haystack) { }
void main ()
{
    find(haystack: "abcdefg",
           needle: "def");
}

dmd can now helpfully point out:

namedarg_haystack.d(6): Error: function namedarg_haystack.find (string needle, string haystack) is not callable using argument types (haystack:string,needle:string)

Nice. This has a different feel to it than where the compiler just 'makes it right' for you, but I still really like it. Super readable.

Reply via email to