www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Optional arguments/parameters

reply "Wanderer" <no-reply no-reply.org> writes:
Java misses this feature badly, forcing programmers to copy-paste 
bloated code (constructor A calls constructor B with fewer 
arguments, constructor B calls constructor C etc, same thing with 
methods). Please tell me, does D support this feature?

int myNiceFunc(double a, double b=0, int c=0) {...}

auto n = myNiceFunc(100);
May 28 2014
next sibling parent "Namespace" <rswhite4 googlemail.com> writes:
On Wednesday, 28 May 2014 at 11:54:30 UTC, Wanderer wrote:
 Java misses this feature badly, forcing programmers to 
 copy-paste bloated code (constructor A calls constructor B with 
 fewer arguments, constructor B calls constructor C etc, same 
 thing with methods). Please tell me, does D support this 
 feature?

 int myNiceFunc(double a, double b=0, int c=0) {...}

 auto n = myNiceFunc(100);

Default parameters? Of course. Every good language does that. :)
May 28 2014
prev sibling next sibling parent "Rene Zwanenburg" <renezwanenburg gmail.com> writes:
On Wednesday, 28 May 2014 at 11:57:51 UTC, Namespace wrote:
 On Wednesday, 28 May 2014 at 11:54:30 UTC, Wanderer wrote:
 Java misses this feature badly, forcing programmers to 
 copy-paste bloated code (constructor A calls constructor B 
 with fewer arguments, constructor B calls constructor C etc, 
 same thing with methods). Please tell me, does D support this 
 feature?

 int myNiceFunc(double a, double b=0, int c=0) {...}

 auto n = myNiceFunc(100);

Default parameters? Of course. Every good language does that. :)

No named parameters though: int myNiceFunc(double a, double b=0, int c=0) {...} auto n = myNiceFunc(100, c: 5); // Doesn't work
May 28 2014
prev sibling next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Wanderer:

 Please tell me, does D support this feature?

You can answer by yourself similar questions online here: http://dpaste.dzfl.pl/ Bye, bearophile
May 28 2014
prev sibling next sibling parent "Wanderer" <no-reply no-reply.org> writes:
On Wednesday, 28 May 2014 at 13:01:52 UTC, bearophile wrote:
 Wanderer:

 Please tell me, does D support this feature?

You can answer by yourself similar questions online here: http://dpaste.dzfl.pl/ Bye, bearophile

Thanks! :-)
May 28 2014
prev sibling next sibling parent Ary Borenszweig <ary esperanto.org.ar> writes:
On 5/28/14, 8:54 AM, Wanderer wrote:
 Java misses this feature badly, forcing programmers to copy-paste
 bloated code (constructor A calls constructor B with fewer arguments,
 constructor B calls constructor C etc, same thing with methods). Please
 tell me, does D support this feature?

 int myNiceFunc(double a, double b=0, int c=0) {...}

 auto n = myNiceFunc(100);

Regarding this, is this supposed to work? --- import std.stdio; class Foo { int bar() { return 1; } int foo(int x = bar()) { return x; } } void main() { new Foo().foo(); } --- I get: coco.d(8): Error: need 'this' to access member bar But there is a 'this' in that place (it's not a static function). Should I file this as a bug?
May 28 2014
prev sibling parent "Dicebot" <public dicebot.lv> writes:
On Wednesday, 28 May 2014 at 17:11:09 UTC, Ary Borenszweig wrote:
 But there is a 'this' in that place (it's not a static 
 function).

 Should I file this as a bug?

I can't find exact place in spec but AFAIK default arguments in D are evaluated in context of call scope, same as in C. Could have detected the problem earlier with better error message though.
May 28 2014