Refactoring Code

Paul Backus snarwin at gmail.com
Sun Mar 3 02:05:10 UTC 2024


On Sunday, 3 March 2024 at 00:44:44 UTC, claptrap wrote:
> Why would you use a bunch of daisy chained conditionals instead 
> of a switch?
>
>     with (Id) with (CppOperator) switch (id)
>     {
>         case _cast  : return Cast;
>         case assign : return Assign;
>         case eq     : return Eq;
>
> etc..
>
> IMO that is clearer, and likely faster, unless the compiler is 
> smart enough to transform the daisy chain into a switch? What's 
> the threshold on DMD for using a jump table? I think it's 3 or 
> 4 on LDC.

While Id looks like an enum, it is actually a struct with a bunch 
of static member variables, and the type of those static 
variables is "class Identifier." You can't switch on a class 
instance.


More information about the Digitalmars-d mailing list