Thread overview | |||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
March 21, 2004 Making optional arguments | ||||
---|---|---|---|---|
| ||||
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. |
March 21, 2004 Re: Making optional arguments | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dn7 | 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 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. |
March 21, 2004 Re: Making optional arguments | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matthew | 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.
>
|
March 21, 2004 Re: Making optional arguments | ||||
---|---|---|---|---|
| ||||
Posted in reply to C. Sauls | 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/ |
March 21, 2004 Re: Making optional arguments | ||||
---|---|---|---|---|
| ||||
Posted in reply to C. Sauls | 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. >> |
March 22, 2004 Re: Making optional arguments | ||||
---|---|---|---|---|
| ||||
Posted in reply to C. Sauls | 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. >> |
March 22, 2004 Re: Making optional arguments | ||||
---|---|---|---|---|
| ||||
Posted in reply to C. Sauls | 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
|
March 22, 2004 Re: Making optional arguments | ||||
---|---|---|---|---|
| ||||
Posted in reply to C. Sauls | > 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 ). 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> 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); > > -C. Sauls > -Invironz > > Matthew wrote: >> D doesn't have default arguments. You need to write a forwarding method, >> taking (usually one) fewer arguments. >> -- D Newsgroup. |
March 22, 2004 Re: Making optional arguments | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dn7 | 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;
}
|
March 22, 2004 Re: Making optional arguments | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dn7 | 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
|
Copyright © 1999-2021 by the D Language Foundation