Thread overview | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
March 21, 2013 this is almost a workaround for the lack of named parameters | ||||
---|---|---|---|---|
| ||||
#!/usr/bin/rdmd void main(string[] arg) { // Observation: I'd like to say: /* auto r = myfunc.call("named") with { z= 2; x = -123; y = 200; } */ // and have it turned into this: with(myfunc) { x = -123; y = 200; z = -20; } auto r = myfunc.call("named"); // Q: is there some way to achieve this? Maybe a variation on with? // Is there a macro facility lurking someplace? Or is there another way to do this? // Thanks! // - J } import std.stdio; struct myfunc_ { // named keyword or named parameters // --the call arguments and their defaults int x=0; int y=0; int z=0; string call(string non_keyword_arg) { writefln("%s: X %s, Y %s, Z %s", non_keyword_arg, x, y, z ); return "yo"; } } myfunc_ myfunc; |
March 21, 2013 Re: this is almost a workaround for the lack of named parameters | ||||
---|---|---|---|---|
| ||||
Posted in reply to J | // CORRECTION: // and have it turned into this: with(myfunc) { z =2; x = -123; y = 200; } auto r = myfunc.call("named"); |
March 21, 2013 Re: this is almost a workaround for the lack of named parameters | ||||
---|---|---|---|---|
| ||||
Posted in reply to J | /* Similarly but different (here I am instantiating a new struct before the call, rather than re-using a global single struct each time), it would be lovely and elegant to say: */ void main(string[] arg) { /* Elegant: auto r = myfunc() with { z= 2; x = -123; y = 200; } */ // and have it lowered to: myfunc tmp; with(tmp) { z= 2; x = -123; y = 200; } auto r = tmp(); } import std.stdio; struct myfunc { // named keyword or named parameters // --the call arguments and their defaults int x=0; int y=0; int z=0; // opCall is sweet... improves upon previous example string opCall(string required_positional_arg = "default pos arg value") { writefln("%s: X %s, Y %s, Z %s", required_positional_arg, x, y, z ); return "yo"; } } |
March 21, 2013 Re: this is almost a workaround for the lack of named parameters | ||||
---|---|---|---|---|
| ||||
Posted in reply to J | On 2013-03-21 19:35, J wrote: Here's another workaround: https://github.com/jacob-carlborg/mambo/blob/master/mambo/util/Reflection.d#L135 -- /Jacob Carlborg |
March 21, 2013 Re: this is almost a workaround for the lack of named parameters | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | Jacob Carlborg:
> Here's another workaround: https://github.com/jacob-carlborg/mambo/blob/master/mambo/util/Reflection.d#L135
Maybe such two links should go in one page of the D wiki about lack of named parameters workarounds.
Eventually D should introduce a syntax for named arguments at the call point plus a syntax to deprecate argument names. It's not the most important thing that D currently lacks, but it's an improvement.
Bye,
bearophile
|
March 21, 2013 Re: this is almost a workaround for the lack of named parameters | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | On Thursday, 21 March 2013 at 19:59:01 UTC, Jacob Carlborg wrote:
> On 2013-03-21 19:35, J wrote:
>
> Here's another workaround: https://github.com/jacob-carlborg/mambo/blob/master/mambo/util/Reflection.d#L135
Intriguing, Jacob! I could learn alot about reflection by studying your code.
How is it installed? I installing by downloading that single file, and with a quick hack to the Reflection.d top three lines (commenting out the module and imports at the top), I tried calling it with this, no luck:
#!/usr/bin/rdmd
import Reflection; // with module and imports commented out...
import std.stdio;
string my(int a, int b) {
writeln(a/b);
return "howdy";
}
void main(string[] arg) {
auto s = callWithNamedArguments(my, `a=3, b=4`);
}
// yields:
$ ./ref.d
./ref.d(13): Error: function ref.my (int a, int b) is not callable using argument types ()
./ref.d(13): Error: expected 2 function arguments, not 0
|
March 21, 2013 Re: this is almost a workaround for the lack of named parameters | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On 2013-03-21 21:33, bearophile wrote: > Maybe such two links should go in one page of the D wiki about lack of > named parameters workarounds. > > Eventually D should introduce a syntax for named arguments at the call > point plus a syntax to deprecate argument names. It's not the most > important thing that D currently lacks, but it's an improvement. What do you think about my suggestion for anonymous structs as named parameters? http://forum.dlang.org/thread/kfbnuc$1cro$1@digitalmars.com?page=1 -- /Jacob Carlborg |
March 21, 2013 Re: this is almost a workaround for the lack of named parameters | ||||
---|---|---|---|---|
| ||||
Posted in reply to J | On 2013-03-21 21:42, J wrote: > Intriguing, Jacob! I could learn alot about reflection by studying your > code. > > How is it installed? I installing by downloading that single file, and > with a quick hack to the Reflection.d top three lines (commenting out > the module and imports at the top), I tried calling it with this, no luck: > > #!/usr/bin/rdmd > > import Reflection; // with module and imports commented out... > import std.stdio; > > string my(int a, int b) { > writeln(a/b); > return "howdy"; > } > > void main(string[] arg) { > > auto s = callWithNamedArguments(my, `a=3, b=4`); > } > > // yields: > > $ ./ref.d > ./ref.d(13): Error: function ref.my (int a, int b) is not callable using > argument types () > ./ref.d(13): Error: expected 2 function arguments, not 0 It's been quite a while since I tested that code. I'll see what I can do. -- /Jacob Carlborg |
March 21, 2013 Re: this is almost a workaround for the lack of named parameters | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | Jacob Carlborg: > What do you think about my suggestion for anonymous structs as named parameters? You show code like: void foo ({ int x, int y } point) { } foo({ y: 5, x: 3 }); D already has two kinds of tuples so I don't want a "third kind" of tuple. What I want is D to manage better the Phobos Tuple we already have, adding an unpacking syntax (http://forum.dlang.org/thread/gridjorxqlpoytuxwpsg@forum.dlang.org ). (This is not supposed to solve the named arguments problem). Bye, bearophile |
March 22, 2013 Re: this is almost a workaround for the lack of named parameters | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On 2013-03-21 22:18, bearophile wrote: > You show code like: > > void foo ({ int x, int y } point) > { > } > > foo({ y: 5, x: 3 }); > > > D already has two kinds of tuples so I don't want a "third kind" of > tuple. What I want is D to manage better the Phobos Tuple we already > have, adding an unpacking syntax > (http://forum.dlang.org/thread/gridjorxqlpoytuxwpsg@forum.dlang.org ). > (This is not supposed to solve the named arguments problem). This is not a tuple, it's an anonymous struct. -- /Jacob Carlborg |
Copyright © 1999-2021 by the D Language Foundation