Thread overview
Named parameters in function call
Sep 08, 2020
Cecil Ward
Sep 08, 2020
Andre Pany
Sep 08, 2020
Cecil Ward
Sep 09, 2020
Paul Backus
Sep 09, 2020
Cecil Ward
September 08, 2020
I can’t remember, do Ada or Modula2 have something like
     myfunc( x => 100, y => 200, color => blue )        [1]
which has named parameters that can be passed in any order.

Does D have anything like this? If not, would anyone support a development like the above [1] ?


If D does not have this, I am wondering about how to write such a thing but the cure might very very easily be worse than the disease. I have little clue here. I have seen a hack for C (written by RevK) that involves assignments to fields in a struct and the struct is then passed to a function.

Something like
    myfunc( { field2: 20, field1: 10, fieldstr : "a string" } )        [2]
and preprocessor trickery was used to get rid of the unsightly { } by making a macro call to a wrapper macro that takes variadic ... arguments.

September 08, 2020
On Tuesday, 8 September 2020 at 07:43:05 UTC, Cecil Ward wrote:
> I can’t remember, do Ada or Modula2 have something like
>      myfunc( x => 100, y => 200, color => blue )        [1]
> which has named parameters that can be passed in any order.
>
> [...]

I hope we have it this year or next year, as we have this DIP
https://www.github.com/dlang/DIPs/tree/master/DIPs%2FDIP1030.md

Kind regards
Andre
September 08, 2020
On Tuesday, 8 September 2020 at 09:40:11 UTC, Andre Pany wrote:
> On Tuesday, 8 September 2020 at 07:43:05 UTC, Cecil Ward wrote:
>> I can’t remember, do Ada or Modula2 have something like
>>      myfunc( x => 100, y => 200, color => blue )        [1]
>> which has named parameters that can be passed in any order.
>>
>> [...]
>
> I hope we have it this year or next year, as we have this DIP
> https://www.github.com/dlang/DIPs/tree/master/DIPs%2FDIP1030.md
>
> Kind regards
> Andre

I wonder if there is any way in which we could combine this with strong typing of some sort (how?) to detect errors such as
    int xcoord;
    int ycoord;

    myfunc( x : ycoord, y : xcoord, color : blue )        [3]

where the arguments are the wrong way around. Would have to change the types of the xcoord and ycoord variables somehow, something I have asked about earlier.
September 09, 2020
On Tuesday, 8 September 2020 at 13:28:22 UTC, Cecil Ward wrote:
>     int xcoord;
>     int ycoord;
You can define your own types, of course:

struct xcoord { int x; alias x this; }
struct ycoord { int y; alias y this; }

void myfunc(xcoord x; ycoord y, color c) {}

September 09, 2020
On Tuesday, 8 September 2020 at 13:28:22 UTC, Cecil Ward wrote:
>
> I wonder if there is any way in which we could combine this with strong typing of some sort (how?) to detect errors such as
>     int xcoord;
>     int ycoord;
>
>     myfunc( x : ycoord, y : xcoord, color : blue )        [3]
>
> where the arguments are the wrong way around. Would have to change the types of the xcoord and ycoord variables somehow, something I have asked about earlier.

import std.typecons: Typedef;

alias XCoord = Typedef!(int, int.init, "XCoord");
alias YCoord = Typedef!(int, int.init, "YCoord");

auto myfunc(XCoord x, YCoord y) { ... }
September 09, 2020
On Wednesday, 9 September 2020 at 11:48:28 UTC, Paul Backus wrote:
> On Tuesday, 8 September 2020 at 13:28:22 UTC, Cecil Ward wrote:
>>
>> I wonder if there is any way in which we could combine this with strong typing of some sort (how?) to detect errors such as
>>     int xcoord;
>>     int ycoord;
>>
>>     myfunc( x : ycoord, y : xcoord, color : blue )        [3]
>>
>> where the arguments are the wrong way around. Would have to change the types of the xcoord and ycoord variables somehow, something I have asked about earlier.
>
> import std.typecons: Typedef;
>
> alias XCoord = Typedef!(int, int.init, "XCoord");
> alias YCoord = Typedef!(int, int.init, "YCoord");
>
> auto myfunc(XCoord x, YCoord y) { ... }

Brilliant. Thank you Paul.