Private default function arguments
BCS
none at anon.com
Fri Jan 15 12:50:02 PST 2010
Hello bearophile,
> My recursive functions sometimes need to keep a bit of state, for
> example an integer that keeps the current "depth" of a tree structure
> that I'm creating or scanning, normally in D I can define the function
> like this:
>
> /// Always use depth=0 at the first call
>
> int foo1(int x, int depth=0) {
>
> ...
>
> foo1(x, depth + 1);
>
> ...
>
> }
>
> The last argument depth must be zero when the function is not called
> by itself, but I must rely on convention and code comments,
My method of choice is:
private int foo1(int x, int depth) { ... }
int foo1(int x) {return foo1(x,0); }
Good inlining might even make this exactly the same as your proposal.
More information about the Digitalmars-d
mailing list