Optional braces
Jacob Carlborg
doob at me.com
Sun Aug 21 11:32:05 PDT 2011
On 2011-08-21 20:02, bearophile wrote:
> Nick Sabalausky:
>
>> Yuck! I'd really hate that.
>
> Recently I have converted this Python code:
>
>
> def foo(sol):
> global best
> if is_solution(sol[-1]):
> if best or len(sol)< len(best):
> best = list(sol)
> else:
> for next in alternatives(sol[-1]):
> if next not in sol:
> foo(sol + [next])
>
>
> To this innocent-looking D code, do you see the problem?
>
> void foo(Pair[] sol) {
> if (isSolution(sol[$-1]))
> if (!best.length || sol.length< best.length)
> best = sol.dup;
> else
> foreach (next; alternatives(sol[$-1]))
> if (!canFind(sol, next))
> foo(sol ~ [next]);
> }
>
>
> There is a situation where I'd like D to require braces:
> http://d.puremagic.com/issues/show_bug.cgi?id=4375
>
> Bye,
> bearophile
Yes, the else is for the second if-statement, not the first one. That's
why I always use braces when I have nested if-statements, at least for
the outer if-statement.
--
/Jacob Carlborg
More information about the Digitalmars-d
mailing list