Re: Named parameters in function call

2020-09-09 Thread Cecil Ward via Digitalmars-d-learn

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.


Re: Named parameters in function call

2020-09-09 Thread Paul Backus via Digitalmars-d-learn

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) { ... }


Re: Named parameters in function call

2020-09-09 Thread Dominikus Dittes Scherkl via Digitalmars-d-learn

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) {}



Re: Named parameters in function call

2020-09-08 Thread Cecil Ward via Digitalmars-d-learn

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.


Re: Named parameters in function call

2020-09-08 Thread Andre Pany via Digitalmars-d-learn

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