'goto', as an indicator of good language

Nick Treleaven nick at geany.org
Fri Sep 9 15:40:14 UTC 2022


On Friday, 9 September 2022 at 14:22:51 UTC, Steven Schveighoffer 
wrote:
>
> Instead of switch, you can use:
> ```d
> do {
>    ...
> } while(false);
> ```
>
> And then use a break inside.

Not the same, continue applies to the do then, not an outer loop. 
Also not clear until the end of the construct that it's not a 
loop.

> But honestly, all this looks like "I want to avoid goto at all 
> costs". The original looks better to me.

It's easier to reason about switch because you don't have to read 
the whole function looking for the matching label.

> Another possibility that uses goto, but might be preferred, is:
>
> ```d
>     if (FuncDeclaration func = sc.parent.isFuncDeclaration())
>     {
>         tm.symtab = func.localsymtab;
>         if (!tm.symtab)
>         {
>             // Inside template constraint, symtab is not set 
> yet.
>             goto L1;
>         }
>     }
>     else
>     {
>         tm.symtab = sc.parent.isScopeDsymbol().symtab;
>     }
>     assert(tm.symtab);
>     tm.ident = Identifier.generateId(s, tm.symtab.length + 1);
>     tm.symtab.insert(tm);
> L1:
> ```

That's an improvement.




More information about the Digitalmars-d mailing list