'goto', as an indicator of good language

Steven Schveighoffer schveiguy at gmail.com
Fri Sep 9 14:22:51 UTC 2022


On 9/9/22 10:03 AM, Nick Treleaven wrote:
> I'd prefer to write it as:
> ```d
>      switch
>      {
>          if (FuncDeclaration func = sc.parent.isFuncDeclaration())
>          {
>              tm.symtab = func.localsymtab;
>              if (!tm.symtab) break;
>              // Inside template constraint, symtab is not set yet.
>          }
>          else
>              tm.symtab = sc.parent.isScopeDsymbol().symtab;
>      //L1:
>          assert(tm.symtab);
>          tm.ident = Identifier.generateId(s, tm.symtab.length + 1);
>          tm.symtab.insert(tm);
>      }
> ```
> Where `switch` works like `switch(0) default:` today.
> 
> Using the function literal call means you can't return from the 
> containing function, which may be needed.
> 

Instead of switch, you can use:
```d
do {
    ...
} while(false);
```

And then use a break inside. But honestly, all this looks like "I want 
to avoid goto at all costs". The original looks better to me.

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:
```

-Steve


More information about the Digitalmars-d mailing list