digitalmars.D.learn - Named parameters in function call
- Cecil Ward (16/16) Sep 08 2020 I can’t remember, do Ada or Modula2 have something like
- Andre Pany (5/9) Sep 08 2020 I hope we have it this year or next year, as we have this DIP
- Cecil Ward (9/19) Sep 08 2020 I wonder if there is any way in which we could combine this with
- Dominikus Dittes Scherkl (5/7) Sep 09 2020 You can define your own types, of course:
- Paul Backus (5/13) Sep 09 2020 import std.typecons: Typedef;
- Cecil Ward (2/18) Sep 09 2020 Brilliant. Thank you Paul.
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.
Sep 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
Sep 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 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.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
Sep 08 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) {}
Sep 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) { ... }
Sep 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:Brilliant. Thank you Paul.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) { ... }
Sep 09 2020









Dominikus Dittes Scherkl <dominikus scherkl.de> 