foreach ... else statement

Don nospam at nospam.com
Mon Jan 5 03:17:47 PST 2009


grauzone wrote:
> bearophile wrote:
>> Don:
>>> Actually Walter loves goto, so DMD copes really well with it.
>>
>> When possible it's better to use structured programming and avoid 
>> gotos. That construct can avoid gotos in some common situations.
>> And regarding the compiler back-end, I think it's also better to start 
>> thinking what's good for LDC :-)
> 
> I don't know how relevant this is, but: LLVM uses SSA for registers, and 
> it seems to be simpler to convert code to SSA if there are no gotos:
> 
> "We show that it is possible to generate SSA form in a single pass (even 
> during parsing) if the program contains only structured control flow 
> (i.e., no gotos). For such programs the dominator tree can be built on 
> the fly, too."
> 
> http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.45.4503

It's possible to create grotesque configurations of 'goto' which are 
extremely difficult to analyze. But most uses of goto are simple.

> I also think that almost all common uses of gotos could be replaced by 
> introducing some new statements for structured control flow. Like 
> allowing the programmer to jump to the begin or end of a block using 
> break/continue, similar to loops. For example, in the Linux kernel, they 
> do the following for error handling:
> 
> void somefunction() {
>     do_stuff();
>     if (error)
>         goto error_exit:
>     do_more_stuff();
> 
>     return;
> 
> error_exit:
>     handle_error();
> }
> 
> This could be replaced by something like this:
> 
> void somefunction() {
>     error_exit: {
>         do_stuff();
>          if (error)
>             break error_exit;
>         do_more_stuff();
> 
>         return;
>     }
>     handle_error();
> }
> 
> D's scope can do the same thing.



More information about the Digitalmars-d mailing list