www.digitalmars.com         C & C++   DMDScript  

D - Making optional arguments

reply Dn7 <Dn7_member pathlink.com> writes:
Sorry for being a noob I guess. But have I been doing something wrong or doesn't

int foo(int bar = 10)
{ }

work yet? I sure do hope it get's added any time soon if it doesn't work yet in
some obscure way(I like that feature 8)). Currently I have a workaround like

int foo(int bar)
{  return bar;
}

int foo()
{  return foo(10);
}

which sucks balls for functions with lengthy parameter lists.

That's right. Balls.
Mar 21 2004
next sibling parent reply "Matthew" <matthew stlsoft.org> writes:
D doesn't have default arguments. You need to write a forwarding method,
taking (usually one) fewer arguments.


"Dn7" <Dn7_member pathlink.com> wrote in message
news:c3l51n$3171$1 digitaldaemon.com...
 Sorry for being a noob I guess. But have I been doing something wrong or

 int foo(int bar = 10)
 { }

 work yet? I sure do hope it get's added any time soon if it doesn't work

 some obscure way(I like that feature 8)). Currently I have a workaround

 int foo(int bar)
 {  return bar;
 }

 int foo()
 {  return foo(10);
 }

 which sucks balls for functions with lengthy parameter lists.

 That's right. Balls.

Mar 21 2004
parent reply "C. Sauls" <ibisbasenji yahoo.com> writes:
Which I, for one, think needs addressing.  Default/optional parameters 
can be quite useful (see for example the Python and C++ ports of 
wxWidgets).  Plus, it seems to me like it could lead to smaller 
executables since the code for the forwarding function would be gone... 
or is that optimized out by DMD?  W?  I'd also love to see Python-style 
keyword arguments.

I think we discussed this once before and got stuck on what a good 
format would be...  Maybe something like:

int foo(int a, int b = 0) // b is defaulted to 0
{ ... }

int bar, x;
// ...
x = foo(bar); //foo(a, 0)
x = foo(b : 10, a : bar) //keyword args

And I think I recall someone proposing something like:
int foo(int a, int b = 0, int c) { ... }

Which could be called like either of:
x = foo(1,,1);
x = foo(a : 1, c : 1);

-C. Sauls
-Invironz

Matthew wrote:
 D doesn't have default arguments. You need to write a forwarding method,
 taking (usually one) fewer arguments.
 

Mar 21 2004
next sibling parent J C Calvarese <jcc7 cox.net> writes:
C. Sauls wrote:
 Which I, for one, think needs addressing.  Default/optional parameters 

I really want D to have optional arguments. I like to use them in VB.
 can be quite useful (see for example the Python and C++ ports of 
 wxWidgets).  Plus, it seems to me like it could lead to smaller 
 executables since the code for the forwarding function would be gone... 
 or is that optimized out by DMD?  W?  I'd also love to see Python-style 
 keyword arguments.
 
 I think we discussed this once before and got stuck on what a good 
 format would be...  Maybe something like:

Indeed, this has been discussed this before. I suspect it will continue to come up until we've persuaded Walter to implement it. It seems inheritance and function overload issues are part of why Walter hasn't implemented them yet (and it's not something he uses as part of his coding style). In case someone is curious, I've developed a list of some of the past threads: http://www.wikiservice.at/d/wiki.cgi?FeatureRequestList/DefaultArguments
 int foo(int a, int b = 0) // b is defaulted to 0
 { ... }
 
 int bar, x;
 // ...
 x = foo(bar); //foo(a, 0)
 x = foo(b : 10, a : bar) //keyword args
 
 And I think I recall someone proposing something like:
 int foo(int a, int b = 0, int c) { ... }
 
 Which could be called like either of:
 x = foo(1,,1);
 x = foo(a : 1, c : 1);
 
 -C. Sauls
 -Invironz
 
 Matthew wrote:
 
 D doesn't have default arguments. You need to write a forwarding method,
 taking (usually one) fewer arguments.


-- Justin http://jcc_7.tripod.com/d/
Mar 21 2004
prev sibling next sibling parent reply Dn7 <Dn7_member pathlink.com> writes:
Those look like they would work. I don't like the ": defvalue" one though. I
actually have an idea that would handle it on a less transperant way, but it
would involve a whole new keyword and it's sort of, well, theoratical and I
really have no idea on how these kind of complex things work. Anyway:

void foo(optional int bar)
{
printf("We have bar");
version(bar) // actually make this something sensible.
{
printf(" and it is %i", bar);
}
printf(".");
}

8) whacky.

In article <c3l5qq$u8$1 digitaldaemon.com>, C. Sauls says...
Which I, for one, think needs addressing.  Default/optional parameters 
can be quite useful (see for example the Python and C++ ports of 
wxWidgets).  Plus, it seems to me like it could lead to smaller 
executables since the code for the forwarding function would be gone... 
or is that optimized out by DMD?  W?  I'd also love to see Python-style 
keyword arguments.

I think we discussed this once before and got stuck on what a good 
format would be...  Maybe something like:

int foo(int a, int b = 0) // b is defaulted to 0
{ ... }

int bar, x;
// ...
x = foo(bar); //foo(a, 0)
x = foo(b : 10, a : bar) //keyword args

And I think I recall someone proposing something like:
int foo(int a, int b = 0, int c) { ... }

Which could be called like either of:
x = foo(1,,1);
x = foo(a : 1, c : 1);

-C. Sauls
-Invironz

Matthew wrote:
 D doesn't have default arguments. You need to write a forwarding method,
 taking (usually one) fewer arguments.
 


Mar 21 2004
parent =?ISO-8859-1?Q?Sigbj=F8rn_Lund_Olsen?= <sigbjorn lundolsen.net> writes:
Dn7 wrote:
 Those look like they would work. I don't like the ": defvalue" one though. I
 actually have an idea that would handle it on a less transperant way, but it
 would involve a whole new keyword and it's sort of, well, theoratical and I
 really have no idea on how these kind of complex things work. Anyway:
 
 void foo(optional int bar)
 {
 printf("We have bar");
 version(bar) // actually make this something sensible.
 {
 printf(" and it is %i", bar);
 }
 printf(".");
 }
 
 8) whacky.

Ah. What you're asking for isn't parameters with default values, it's *optional* parameters. In that case, overloading will do the job perfectly well, ie: void foo(int bar) { printf("We have bar and it is %i.", bar); } void foo() { printf("We have bar."); } Cheers, Sigbjørn Lund Olsen
Mar 22 2004
prev sibling next sibling parent Ben R <Ben_member pathlink.com> writes:
In article <c3l5qq$u8$1 digitaldaemon.com>, C. Sauls says...

I vote that all the following is so intuitively spot-on (especially in light of
other language's conventions)  that it is irresistable...

Which I, for one, think needs addressing.  Default/optional parameters 
can be quite useful (see for example the Python and C++ ports of 
wxWidgets).  Plus, it seems to me like it could lead to smaller 
executables since the code for the forwarding function would be gone... 
or is that optimized out by DMD?  W?  I'd also love to see Python-style 
keyword arguments.

I think we discussed this once before and got stuck on what a good 
format would be...  Maybe something like:

int foo(int a, int b = 0) // b is defaulted to 0
{ ... }

int bar, x;
// ...
x = foo(bar); //foo(a, 0)
x = foo(b : 10, a : bar) //keyword args

And I think I recall someone proposing something like:
int foo(int a, int b = 0, int c) { ... }

Which could be called like either of:
x = foo(1,,1);
x = foo(a : 1, c : 1);

-C. Sauls
-Invironz

Matthew wrote:
 D doesn't have default arguments. You need to write a forwarding method,
 taking (usually one) fewer arguments.
 


Mar 21 2004
prev sibling next sibling parent Andy Friesen <andy ikagames.com> writes:
C. Sauls wrote:
 Which I, for one, think needs addressing.  Default/optional parameters 
 can be quite useful (see for example the Python and C++ ports of 
 wxWidgets).  Plus, it seems to me like it could lead to smaller 
 executables since the code for the forwarding function would be gone... 
 or is that optimized out by DMD?  W?  I'd also love to see Python-style 
 keyword arguments.
 
 I think we discussed this once before and got stuck on what a good 
 format would be...  Maybe something like:
 
 int foo(int a, int b = 0) // b is defaulted to 0
 { ... }
 
 int bar, x;
 // ...
 x = foo(bar); //foo(a, 0)
 x = foo(b : 10, a : bar) //keyword args
 
 And I think I recall someone proposing something like:
 int foo(int a, int b = 0, int c) { ... }
 
 Which could be called like either of:
 x = foo(1,,1);
 x = foo(a : 1, c : 1);

How would this work in the context of polymorphic functions? C++ default arguments are terrible in that the default is decided by the type that the object is cast as which can lead for some scary weirdness if you're not careful. In the big scheme of things, it's not a big deal to a small handful of simple overloads to deal with default values. I think that it'd be a good thing. I just don't know how realistic it is for a statically compiled language like D. -- andy
Mar 21 2004
prev sibling parent reply C <dont respond.com> writes:
 Plus, it seems to me like it could lead to smaller executables since t=

 code for the forwarding function would be gone... or is that optimized=

 out by DMD?

Nope :S ( obj2asm ). I whole heartedly agree, if it means pushing back 1.0 specs a little bit= = Im cool with that. C On Sun, 21 Mar 2004 16:43:33 -0600, C. Sauls <ibisbasenji yahoo.com> wro= te:
 Which I, for one, think needs addressing.  Default/optional parameters=

 can be quite useful (see for example the Python and C++ ports of =

 wxWidgets).  Plus, it seems to me like it could lead to smaller =

 executables since the code for the forwarding function would be gone..=

 or is that optimized out by DMD?  W?  I'd also love to see Python-styl=

 keyword arguments.

 I think we discussed this once before and got stuck on what a good =

 format would be...  Maybe something like:

 int foo(int a, int b =3D 0) // b is defaulted to 0
 { ... }

 int bar, x;
 // ...
 x =3D foo(bar); //foo(a, 0)
 x =3D foo(b : 10, a : bar) //keyword args

 And I think I recall someone proposing something like:
 int foo(int a, int b =3D 0, int c) { ... }

 Which could be called like either of:
 x =3D foo(1,,1);
 x =3D foo(a : 1, c : 1);

 -C. Sauls
 -Invironz

 Matthew wrote:
 D doesn't have default arguments. You need to write a forwarding meth=


 taking (usually one) fewer arguments.


-- = D Newsgroup.
Mar 21 2004
parent Ilya Minkov <minkov cs.tum.edu> writes:
C schrieb:

 Plus, it seems to me like it could lead to smaller executables since 
 the code for the forwarding function would be gone... or is that 
 optimized out by DMD?

Nope :S ( obj2asm ).

Tried in release mode with function-level linkage? Ok, it won't work for class member functions anyway. Anyway, i think Walter said that this is really intended to be eliminated, but i think it's not going to be fully possible until someone writes an efficient eliminator like the one in Sather. I recall that Walter was against default arguments because they can be problematic in derived classes and so on. -eye
Mar 22 2004
prev sibling parent Ben Hinkle <bhinkle4 juno.com> writes:
On Sun, 21 Mar 2004 22:30:16 +0000 (UTC), Dn7
<Dn7_member pathlink.com> wrote:

Sorry for being a noob I guess. But have I been doing something wrong or doesn't

int foo(int bar = 10)
{ }

work yet? 

Another option besides writing multiple functions is to declare the defaults and have the caller explicitly use them (which is what the compiler would do except that it could make up a super-secret name for the default data). It even lets you add "optional" arguments before the end of the argument list. Typical D style is to be explicit in code anyway, so this isn't too bad. A nice naming scheme would go a long way to making it usable. The example below uses funcname_varname_opt. I briefly tried getting fancy with "." but it all became too verbose. /** Test function that takes a couple of optional args. * Blah blah. * * \param x optional blah blah. Default value is test_x_opt. * \param y optional string. Default value is test_y_opt. */ void test(int x, // = test_x_opt char[] y // = test_y_opt ) { printf("x = %d\n", x); printf("y = %.*s\n\n", y); } /* default values for optional parameters */ const int test_x_opt = 10; const char[] test_y_opt = "hello"; /* User code would look something like this: */ int main() { test(test_x_opt, "boo"); test(42, test_y_opt); return 0; }
Mar 21 2004