Optional arguments/parameters

Ary Borenszweig via Digitalmars-d digitalmars-d at puremagic.com
Wed May 28 10:11:07 PDT 2014


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?


More information about the Digitalmars-d mailing list