Jump to page: 1 2
Thread overview
Refactoring Code
Mar 03
claptrap
Mar 03
ryuukk_
Mar 04
kdevel
Mar 06
ryuukk_
Mar 06
Basile B.
March 02
https://github.com/dlang/dmd/pull/16280/files

Sometimes if you stare at code long enough, its underlying simplicity emerges.
March 03
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.
March 03
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.
March 03
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).
March 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
March 03
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.
March 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:

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

March 04

On Sunday, 3 March 2024 at 09:59:28 UTC, ryuukk_ wrote:

>
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?

March 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.

March 06

On Monday, 4 March 2024 at 22:52:18 UTC, kdevel wrote:

>

On Sunday, 3 March 2024 at 09:59:28 UTC, ryuukk_ wrote:

>
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"

« First   ‹ Prev
1 2