I can create a callable struct that keeps depth
as an attribute (code not tested):
struct foo2 {
int depth = 0;
int opCall(int x) {
...
}
}
This foo2 may even be a little faster because less data is passed to the
function (callable struct) argument.
But another possible solution
0;
> int opCall(int x) {
> ...
> }
> }
>
> This foo2 may even be a little faster because less data is passed to the
> function (callable struct) argument.
>
> But another possible solution is to have private default function arguments:
>
> int fo
Clemens Wrote:
> Why not this?
>
> void foo(int x)
> {
> void fooImpl(int x, int depth);
> {
> // ...
> }
>
> fooImpl(x, 0);
> }
>
Ah, and I guess you should make fooImpl static if you don't intend to access
foo's x parameter directly.
Clemens:
> void foo(int x)
> {
> void fooImpl(int x, int depth);
> {
> // ...
> }
>
> fooImpl(x, 0);
> }
OK, I'll use a static nested function then, it's not as clean as adding a
"private" attribute, but probably there's not enough need to add other features
to the lang
bearophile:
> OK, I'll use a static nested function then, it's not as clean as adding a
> "private" attribute, but probably there's not enough need to add other
> features to the language:
>
> int foo4(int x) {
> int foo4inner(int x, depth=0) {
It's not as clean indeed, there's a little bug t
0;
> int opCall(int x) {
> ...
> }
> }
>
> This foo2 may even be a little faster because less data is passed to the
> function (callable struct) argument.
>
> But another possible solution is to have private default function arguments:
>
> int fo
Fri, 15 Jan 2010 04:38:19 -0500, bearophile wrote:
> Clemens:
>
>> void foo(int x)
>> {
>> void fooImpl(int x, int depth);
>> {
>> // ...
>> }
>>
>> fooImpl(x, 0);
>> }
>
> OK, I'll use a static nested function then, it's not as clean as adding
> a "private" attribute, b
retard:
>But the private modifier is something that comes from the object oriented
>approach.<
Yes, I agree, adding "private" to recursive function attributes is a cute nice
borrowed from the OOP. It beats the other three alternatives I've shown.
Bye,
bearophile
Daniel Murphy:
> private struct privateint { int i; alias i this; };
> void foo(int x, privateint y = privateint(0))
> {
> ...
> }
>
> It's ugly, but should give reasonable error messages.
It looks like giving error messages if it's located in another module. And I
agree, it's ugly. In real
Fri, 15 Jan 2010 07:10:00 -0500, bearophile wrote:
> retard:
>>But the private modifier is something that comes from the object
>>oriented approach.<
>
> Yes, I agree, adding "private" to recursive function attributes is a
> cute nice borrowed from the OOP. It beats the other three alternatives
>
retard:
> Beats?
Yes, in my opinion it's better than the alternatives. In the end here what I
will use in normal code is the basic strategy of using just comments.
>Documentation tools need to be aware of this.
D uses ddoc, as basic solution for this.
>Requires compiler patch.
As any soluti
bearophile Wrote:
> But it also simplifies code, makes it a bit safer, avoiding some possible
> bugs both of the original problem and created by the alternative solutions.
What problems does the static inner function idiom create? It's clean, safe,
performant, works with the current language an
Clemens:
> What problems does the static inner function idiom create? It's clean, safe,
> performant,<
Clean: requiring 3-4 extra lines of code and a second name (for the inner
function) is less clean. Generally it's better to minimize the number of names
that you need to invent in a program.
ossible solution is to have private default function arguments:
int foo3(int x, private int depth=0) {
...
foo3(x+1); // OK
foo3(x, depth + 1); // OK
...
}
void main() {
int r = foo3(5); // OK
int r = foo3(5, 1); // Error
int r = foo3(5, 0); // Error
}
Now the programmer is
bearophile Wrote:
> int foo3(int x, private int depth=0) {
> ...
> foo3(x+1); // OK
> foo3(x, depth + 1); // OK
> ...
> }
> void main() {
> int r = foo3(5); // OK
> int r = foo3(5, 1); // Error
> int r = foo3(5, 0); // Error
> }
>
> Now the programmer is allowed to give/spec
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, i
"Jesse Phillips" wrote in message
news:hiqfel$1i3...@digitalmars.com...
> bearophile Wrote:
>
>> int foo3(int x, private int depth=0) {
>> ...
>> foo3(x+1); // OK
>> foo3(x, depth + 1); // OK
>> ...
>> }
>> void main() {
>> int r = foo3(5); // OK
>> int r = foo3(5, 1); // Error
>> i
On 15/01/10 08:25, bearophile wrote:
int foo3(int x, private int depth=0) {
...
foo3(x+1); // OK
foo3(x, depth + 1); // OK
...
}
void main() {
int r = foo3(5); // OK
int r = foo3(5, 1); // Error
int r = foo3(5, 0); // Error
}
Does this not achieve the same effect? (OK, the
Robert Clipsham wrote:
> On 15/01/10 08:25, bearophile wrote:
>> int foo3(int x, private int depth=0) {
>>...
>>foo3(x+1); // OK
>>foo3(x, depth + 1); // OK
>>...
>> }
>> void main() {
>>int r = foo3(5); // OK
>>int r = foo3(5, 1); // Error
>>int r = foo3(5, 0); // Error
On 16/01/10 14:39, "Jérôme M. Berger" wrote:
Two words: stack overflow. Even if you fix your function to avoid
the stack overflow, this would fail in multithread environments (or
be too slow if depth is made thread local).
Jerome
Ah, thanks :)
Seems in my simple single
20 matches
Mail list logo