www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Refactoring Code

reply Walter Bright <newshound2 digitalmars.com> writes:
https://github.com/dlang/dmd/pull/16280/files

Sometimes if you stare at code long enough, its underlying simplicity emerges.
Mar 02
next sibling parent reply claptrap <clap trap.com> writes:
On Saturday, 2 March 2024 at 20:27:59 UTC, Walter Bright wrote:
 https://github.com/dlang/dmd/pull/16280/files

 Sometimes if you stare at code long enough, its underlying 
 simplicity emerges.
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.
Mar 02
parent Paul Backus <snarwin gmail.com> writes:
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.
Mar 02
prev sibling next sibling parent reply Max Samukha <maxsamukha gmail.com> writes:
On Saturday, 2 March 2024 at 20:27:59 UTC, Walter Bright wrote:
 https://github.com/dlang/dmd/pull/16280/files

 Sometimes if you stare at code long enough, its underlying 
 simplicity emerges.
I don't see how that is simpler than the original (given the lazily initialized dynamic array is replaced with a static one and the loop is replaced with an FP equivalent).
Mar 02
parent reply Walter Bright <newshound2 digitalmars.com> writes:
On 3/2/2024 9:48 PM, Max Samukha wrote:
 I don't see how that is simpler than the original (given the lazily
initialized 
 dynamic array is replaced with a static one and the loop is replaced with an
FP 
 equivalent).
1. no need to manually verify that the array is in the same order as the enum. 2. no variables are declared 3. there's no concern about thread safety with the static variable initialization 4. no cast conversion, which has to be checked manually
Mar 02
parent reply Max Samukha <maxsamukha gmail.com> writes:
On Sunday, 3 March 2024 at 07:30:53 UTC, Walter Bright wrote:
 1. no need to manually verify that the array is in the same 
 order as the enum.
Fair.
 2. no variables are declared
True.
 3. there's no concern about thread safety with the static 
 variable initialization
The array doesn't need to be lazily initialized (unless I miss some important detail).
 4. no cast conversion, which has to be checked manually
Right. I could list a number of random points in favor of the original.
Mar 03
parent reply ryuukk_ <ryuukk.dev gmail.com> writes:
On Sunday, 3 March 2024 at 09:47:28 UTC, Max Samukha wrote:
 I could list a number of random points in favor of the original.
There was nothing good about the original, the new code is so much better and easier to read Although, i don't like the double `with`, i'd prefer to do it this way personally: ```D package CppOperator isCppOperator(const scope Identifier id) { return switch (id) { :_cast => :Cast; :assign => :Assign; :eq => :Eq; :index => :Index; :call => :Call; :opUnary => :Unary; :opBinary => :Binary; :opOpAssign => :OpAssign; else => :Unknown; }; } ``` Symbols won't clash, and it has less noise
Mar 03
parent reply kdevel <kdevel vogtner.de> writes:
On Sunday, 3 March 2024 at 09:59:28 UTC, ryuukk_ wrote:
 ```D
 package CppOperator isCppOperator(const scope Identifier id)
 {
     return switch (id) {
         :_cast       => :Cast;
         :assign      => :Assign;
         :eq          => :Eq;
         :index       => :Index;
         :call        => :Call;
         :opUnary     => :Unary;
         :opBinary    => :Binary;
         :opOpAssign  => :OpAssign;
         else => :Unknown;
     };
 }
 ```

 Symbols won't clash, and it has less noise
The code is tagged as 'D' but what is the real name of the language?
Mar 04
parent ryuukk_ <ryuukk.dev gmail.com> writes:
On Monday, 4 March 2024 at 22:52:18 UTC, kdevel wrote:
 On Sunday, 3 March 2024 at 09:59:28 UTC, ryuukk_ wrote:
 ```D
 package CppOperator isCppOperator(const scope Identifier id)
 {
     return switch (id) {
         :_cast       => :Cast;
         :assign      => :Assign;
         :eq          => :Eq;
         :index       => :Index;
         :call        => :Call;
         :opUnary     => :Unary;
         :opBinary    => :Binary;
         :opOpAssign  => :OpAssign;
         else => :Unknown;
     };
 }
 ```

 Symbols won't clash, and it has less noise
The code is tagged as 'D' but what is the real name of the language?
"i'd prefer to do it this way" Contraction for "'i would' prefer", i should have used proper english, my bad That's how improvements happen, you find an annoying it, and suggest something to make it better As for the "tagged as 'D'", it's simply just to give the post some colors, i am a person with taste and i care for the reader's pleasure and convenience "switch as expression", and "safer pattern match"
Mar 06
prev sibling next sibling parent Basile B. <b2.temp gmx.com> writes:
On Saturday, 2 March 2024 at 20:27:59 UTC, Walter Bright wrote:
 https://github.com/dlang/dmd/pull/16280/files

 Sometimes if you stare at code long enough, its underlying 
 simplicity emerges.
that's exactly how I get that the "dot assign" is a thing. ``` current = current.next; ``` is actually a binary assign which can be rewritten ``` current .= next; ``` Staring at code involving trees and stacks. Keep calm ladies, I dont plan to propose that for D, it's just an example of how staring at code is actually a good thing.
Mar 05
prev sibling parent Salih Dincer <salihdb hotmail.com> writes:
On Saturday, 2 March 2024 at 20:27:59 UTC, Walter Bright wrote:
 https://github.com/dlang/dmd/pull/16280/files

 Sometimes if you stare at code long enough, its underlying 
 simplicity emerges.
The same ```if()``` blocks will be created for the ```switch case``` for the already generated assembly code; ```switch case``` can also be used if you're happy to see it this way: ```d with (Id) with (CppOperator) { switch(id) { case _cast : return Cast; case assign : return Assign; case eq : return Eq; case index : return Index; case call : return Call; case opUnary : return Unary; case opBinary : return Binary; case opOpAssign : return OpAssign; default : return Unknown; } } ``` SDB 79
Mar 21