Struggling to implement parallel foreach...

Manu turkeyman at gmail.com
Fri Jun 14 18:51:38 UTC 2019


On Fri, Jun 14, 2019 at 8:05 AM Kagamin via Digitalmars-d
<digitalmars-d at puremagic.com> wrote:
>
> On Friday, 14 June 2019 at 09:04:42 UTC, Manu wrote:
> > Right, exactly... the compile error should be at the assignment
> > of the
> > not-shared closure to the shared delegate. The error would be
> > something like "can't assign `Context*` to `shared(Context)*`".
> > So, the function can certainly exist, but for shared, you
> > should get
> > an error when you attempt to call it passing the closure to the
> > function.
>
> The shared function attribute means that its context is shared
> similar to object methods, the error is an attempt to implicitly
> leak unshared data into that shared context, that's why indeed
> function itself is incorrect. Declare the variable as shared and
> it will work.

No, you've misunderstood. The qualifier does NOT apply to the context
as it should, that's the issue I'm reporting.
Try it with const, it's simpler to understand. You can incorrectly
mutate x from a const context.

A local function is essentially:
void localFun(Context*, Args...);

A const local function should be:
void localFun(const(Context)*, Args...) const  // <- const applies to
the context pointer, as usual.

That does not appear to be the case however.
For example:

void test()
{
  int x;
  void fun() const
  {
    pragma(msg, typeof(x)); // should print `const(int)` because
typeof(const(Context*).x) == const(int), but it incorrectly prints
`int`
    ++x; // <- should be an error, but the context pointer is not
const, so this compiles!
  }
}

The context pointer is missing the qualifier.


More information about the Digitalmars-d mailing list