May 24, 2022

On Tuesday, 24 May 2022 at 07:58:13 UTC, FeepingCreature wrote:

>

This is a bit of a controversial topic. Personally I agree that there should be a closure allocated every loop pass, but every other language allocates them for the whole stackframe, so...

That is patently false. Javascript? C#?

It is true of some languages, such as python, but this is because in python, variable scope is always the top level in the function.

May 24, 2022

On Tuesday, 24 May 2022 at 13:30:15 UTC, zjh wrote:

> >

We should organize the community.

Since we all use d language, you have to feedback and repair it. If you don't make effort to it, you can only see other languages surpass you, and there's nothing you can do.
Since we all use d language, we are all on the same boat.
If D language is not good, everyone has to suffer.
Therefore, the 'd' language community should not be too loose, but should be organized.
Everyone can partially uses their spare time to serve for 'd'.Of course,'D' officials can pay according to their ability.
Without money, you can give honor, right?

D man should organize, otherwise D users are losers.

May 24, 2022

On Tuesday, 24 May 2022 at 13:34:07 UTC, deadalnix wrote:

>

On Tuesday, 24 May 2022 at 07:58:13 UTC, FeepingCreature wrote:

>

This is a bit of a controversial topic. Personally I agree that there should be a closure allocated every loop pass, but every other language allocates them for the whole stackframe, so...

That is patently false. Javascript? C#?

I do recall that C# has the same problem. Or at least had in Unity, ~6 years ago. I work around it this way (pseudocode, didn't test this):

foreach (i; 0..10)
{ auto makeDelegate(size_t closureVar)
  { return ()
    { // closureVar is i, but will be different for each iteration
    };
  };

  doSomethingWithDelegate(makeDelegate(i));
}
May 24, 2022

On Tuesday, 24 May 2022 at 11:51:43 UTC, Dukc wrote:

>

On Tuesday, 24 May 2022 at 10:22:31 UTC, burjui wrote:

>

Why D is unpopular? Because people don't stay. As I can only truly speak for myself, here's the reason why I left:
https://issues.dlang.org/show_bug.cgi?id=2043

This bug is 14 years old already, and the memory-corrupting code still compiles, unless you enable DIP1000 (not a standard yet).

It's becoming one. The latest Dmd, 2.100, already prints a deprecation message about DIP1000-noncompatible code unless you explicitly add -revert=dip1000. And it's quickly becoming stable too. Only three or four releases ago, you continuosly discovered bugs if trying to seriously use DIP1000 but in my experience, there's only a fraction of that anymore.

DIP1000 is bad. It can detect a few trivial case, but beside that, it is unable to detect anything. As a result, it cannot solve this specific problem, only simple instance of it. The expect impact is therefore an increase in the complexity of the language without providing the kind of value you'd expect out of it.

I was told to submit bug report, but this is not a bug. There is no amount of bug that you can fix in DIP1000 to make it do what it is supposed to do. Just like there is no amount of bug report I can submit to a regex that tries to parse D.

I have no doubt this is going to happen though, and it will break tools and code, and it won't bring the value that it is supposed to, because it can't, and validate everything in this thread. I hope you prove me wrong, please do.

May 24, 2022

On Tuesday, 24 May 2022 at 13:49:28 UTC, Dukc wrote:

>

On Tuesday, 24 May 2022 at 13:34:07 UTC, deadalnix wrote:

>

On Tuesday, 24 May 2022 at 07:58:13 UTC, FeepingCreature wrote:

>

This is a bit of a controversial topic. Personally I agree that there should be a closure allocated every loop pass, but every other language allocates them for the whole stackframe, so...

That is patently false. Javascript? C#?

I do recall that C# has the same problem. Or at least had in Unity, ~6 years ago. I work around it this way (pseudocode, didn't test this):

foreach (i; 0..10)
{ auto makeDelegate(size_t closureVar)
  { return ()
    { // closureVar is i, but will be different for each iteration
    };
  };

  doSomethingWithDelegate(makeDelegate(i));
}

This was fixed in C#5, in 2012, so 10 years ago.

May 24, 2022

On Tuesday, 24 May 2022 at 13:34:07 UTC, deadalnix wrote:

>

On Tuesday, 24 May 2022 at 07:58:13 UTC, FeepingCreature wrote:

>

This is a bit of a controversial topic. Personally I agree that there should be a closure allocated every loop pass, but every other language allocates them for the whole stackframe, so...

That is patently false. Javascript? C#?

It is true of some languages, such as python, but this is because in python, variable scope is always the top level in the function.

Javascript, yes:

function foo() {
    var cb = null;
    for (var i = 0; i < 10; i++) {
        if (!cb) {
            cb = function() { console.log("Hello World " + i); };
            cb();
        }
    }
    cb();
}
foo();

Hello World 0
Hello World 10

May 24, 2022

On Tuesday, 24 May 2022 at 15:15:13 UTC, FeepingCreature wrote:

>

Javascript, yes:

function foo() {
    var cb = null;
    for (var i = 0; i < 10; i++) {
        if (!cb) {
            cb = function() { console.log("Hello World " + i); };
            cb();
        }
    }
    cb();
}
foo();

Hello World 0
Hello World 10

Now try this and tell me how it goes:

function foo() {
    var cb = null;
    for (var i = 0; i < 10; i++) {
        let n = i;
        if (!cb) {
            cb = function() { console.log("Hello World " + n); };
            cb();
        }
    }
    cb();
}
foo();

If you are capturing i and mutating it, then i is going to be mutated at the end. It says nothing about captures.

May 24, 2022
On Tuesday, 24 May 2022 at 15:15:13 UTC, FeepingCreature wrote:
> Javascript, yes:

If you use `var`, the variable is hoisted to the top-level scope of the function, so this behavior is then expected.

Use the `let` keyword for a local lexically scope thing, and then you'll see the cb behavior is different.

Since D's scoping is more like `let` than `var`, JS is a point in favor of it being a bug.

(though JS's pattern to make it work anyway is what I also do in D today. It isn't that big of a deal.... though the fact the `immutable` is a lie there is what convinces me it is a bug anyway.)
May 24, 2022
On Tuesday, 24 May 2022 at 15:31:47 UTC, Adam D Ruppe wrote:
> On Tuesday, 24 May 2022 at 15:15:13 UTC, FeepingCreature wrote:
>> Javascript, yes:
>
> If you use `var`, the variable is hoisted to the top-level scope of the function, so this behavior is then expected.
>
> Use the `let` keyword for a local lexically scope thing, and then you'll see the cb behavior is different.
>

While this is true, his example is even worse: the captured variable isn't declared **IN** the loop, so whether `var` or `let` is used changes nothing.
May 24, 2022
On 5/24/2022 12:43 AM, Timon Gehr wrote:
> The original example no longer compiles, there are others in that thread. It has not been fixed!

Other issues should get their own bugzilla issue.