digitalmars.D - Refactoring Code
- Walter Bright (2/2) Mar 02 https://github.com/dlang/dmd/pull/16280/files
- claptrap (13/16) Mar 02 Why would you use a bunch of daisy chained conditionals instead
- Paul Backus (5/17) Mar 02 While Id looks like an enum, it is actually a struct with a bunch
- Max Samukha (4/7) Mar 02 I don't see how that is simpler than the original (given the
- Walter Bright (5/8) Mar 02 1. no need to manually verify that the array is in the same order as the...
- Max Samukha (7/13) Mar 03 True.
- Basile B. (12/15) Mar 05 that's exactly how I get that the "dot assign" is a thing.
- Salih Dincer (22/25) Mar 21 The same ```if()``` blocks will be created for the ```switch
https://github.com/dlang/dmd/pull/16280/files Sometimes if you stare at code long enough, its underlying simplicity emerges.
Mar 02
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
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
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
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
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 declaredTrue.3. there's no concern about thread safety with the static variable initializationThe array doesn't need to be lazily initialized (unless I miss some important detail).4. no cast conversion, which has to be checked manuallyRight. I could list a number of random points in favor of the original.
Mar 03
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
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 noiseThe code is tagged as 'D' but what is the real name of the language?
Mar 04
On Monday, 4 March 2024 at 22:52:18 UTC, kdevel wrote:On Sunday, 3 March 2024 at 09:59:28 UTC, ryuukk_ wrote:"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"```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 noiseThe code is tagged as 'D' but what is the real name of the language?
Mar 06
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
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