www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - suggestion: default values

reply bobef <adas adasd.asdasd> writes:
It would be nice if there was some character like $ or something, if used as
function argument, the default value for this argument to be used. What I mean
is this:

void myfun(int a,char[] b,bool d=false,int e=55){}

void main()
{
    myfun(5,"5",$,$); // which should act like myfun(5,"5",false,55);
}
Apr 19 2007
next sibling parent reply Gregor Richards <Richards codu.org> writes:
bobef wrote:
 It would be nice if there was some character like $ or something, if used as
function argument, the default value for this argument to be used. What I mean
is this:
 
 void myfun(int a,char[] b,bool d=false,int e=55){}
 
 void main()
 {
     myfun(5,"5",$,$); // which should act like myfun(5,"5",false,55);
 }
Probably a better example of why this would be helpful: void myfunction(int a, char[] b, Foo veryComplicatedThing = default, bool d = true) {} void main() { myfun(5,"5",$,false); }
Apr 19 2007
parent Daniel Keep <daniel.keep.lists gmail.com> writes:
Gregor Richards wrote:
 bobef wrote:
 It would be nice if there was some character like $ or something, if
 used as function argument, the default value for this argument to be
 used. What I mean is this:

 void myfun(int a,char[] b,bool d=false,int e=55){}

 void main()
 {
     myfun(5,"5",$,$); // which should act like myfun(5,"5",false,55);
 }
Probably a better example of why this would be helpful: void myfunction(int a, char[] b, Foo veryComplicatedThing = default, bool d = true) {} void main() { myfunction(5,"5",$,false); }
We already have the perfect keyword for this: default. void main() { myfunction(5, "5", default, false); } A little more verbose, but pretty self-explanatory as to what it does. Also, keep in mind that "$" already means array length inside of a slice; I think it would be a bad idea to overload it with a completely different meaning. On a side note, you can do something vaguely similar using templated functions and "typedef int DefaultType; const Default = DefaultType.ini;" like so: const int e_default = 55; void myfunction(T)(T _e=Default) { static if( is( T == DefaultType ) ) e = e_default; else e = e; } Or somesuch; been a while since I did that :) -- Daniel -- int getRandomNumber() { return 4; // chosen by fair dice roll. // guaranteed to be random. } http://xkcd.com/ v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP http://hackerkey.com/
Apr 19 2007
prev sibling parent reply "David B. Held" <dheld codelogicconsulting.com> writes:
bobef wrote:
 It would be nice if there was some character like $ or something, if used as
function argument, the default value for this argument to be used. What I mean
is this:
 
 void myfun(int a,char[] b,bool d=false,int e=55){}
 
 void main()
 {
     myfun(5,"5",$,$); // which should act like myfun(5,"5",false,55);
 }
While some languages provide named parameters, they tend to be langs which encourage functions with many arguments. If you find you are creating functions like this, you should first ask yourself if this is the best way. If you decide that it is, one pattern to use is analogous to the Builder pattern: struct myFunArgs { int a; char[] b; bool d = false; int e = 55; } void myfun(myFunArgs args) { ... } myFunArgs args; args.a = 5; args.b = "5"; myfun(args); This is the *very* Poor Man's named parameters mechanism, but if you have enough parameters to need named ones, then this probably isn't at all burdensome (and may increase clarity). Another alternative, if you are creating a library function that you want to be *very* friendly, you can create a variadic type-safe template function and parse the args inside the function (though this doesn't work very well with args of the same type). Of course, you can use the Perl convention of name, value pairs to make it completely general, but that's not very elegant or idiomatic D style. Dave
Apr 19 2007
parent Leandro Lucarella <llucax gmail.com> writes:
David B. Held, el 19 de abril a las 23:42 me escribiste:
 bobef wrote:
It would be nice if there was some character like $ or something, if used as
function argument, the default value for this argument to be used. What I mean
is this:
void myfun(int a,char[] b,bool d=false,int e=55){}
void main()
{
    myfun(5,"5",$,$); // which should act like myfun(5,"5",false,55);
}
While some languages provide named parameters, they tend to be langs which encourage functions with many arguments. If you find you are creating functions like this, you should first ask yourself if this is the best way. If you decide that it is, one pattern to use is analogous to the Builder pattern: struct myFunArgs { int a; char[] b; bool d = false; int e = 55; } void myfun(myFunArgs args) { ... } myFunArgs args; args.a = 5; args.b = "5"; myfun(args); This is the *very* Poor Man's named parameters mechanism, but if you have enough parameters to need named ones, then this probably isn't at all burdensome (and may increase clarity). Another alternative, if you are creating a library function that you want to be *very* friendly, you can create a variadic type-safe template function and parse the args inside the function (though this doesn't work very well with args of the same type). Of course, you can use the Perl convention of name, value pairs to make it completely general, but that's not very elegant or idiomatic D style.
Another way is to use partial function aplication: void myfun(int a,char[] b,bool d=false,int e=55){} void partial_myfun(int a,char[] b,int e=55) { myfun(a, b, false, e); } partial_myfun(5, "5", 65); // myfun(5, "5", false, 65); I think template black-magic could help to make this a little shorter to use (maybe boost has something like this?) and well, with a little reflection (for default value extraction), you can have a complete automated partial function application, I think. -- LUCA - Leandro Lucarella - Usando Debian GNU/Linux Sid - GNU Generation ------------------------------------------------------------------------ E-Mail / JID: luca lugmen.org.ar GPG Fingerprint: D9E1 4545 0F4B 7928 E82C 375D 4B02 0FE0 B08B 4FB2 GPG Key: gpg --keyserver pks.lugmen.org.ar --recv-keys B08B4FB2 ------------------------------------------------------------------------ La esperanza es una amiga que nos presta la ilusiĆ³n.
Apr 20 2007