Struggling to implement parallel foreach...

Manu turkeyman at gmail.com
Fri Jun 14 21:56:28 UTC 2019


On Fri, Jun 14, 2019 at 1:55 PM Exil via Digitalmars-d
<digitalmars-d at puremagic.com> wrote:
>
> Looks like it was an overlooked detail? There are a few messy
> implementation details with function/delegate. If you take the
> address of a member function, without a variable. You get a
> function, from the looks of it that function can be const, which
> doesn't make sense. But then the fact it is a function and not a
> delegate also doesn't make sense.
>
> struct S {
>     void foo() const {}
> }
>
> pragma(msg, typeof(&S.foo)); // void function() const
>
> Anyways even if you do shared like you would in a struct. You
> can't implicitly convert to shared in the way you can const.

Correct, you can not implicitly convert to shared, the error message
should be "Can not convert `Context*` to `shared(Context)*`", or
something to that effect.
That is the error I expect. But *within* the function, the context is
`shared(Context)*`, and that is transitive as usual.
The error should be when making the CALL to the function failing to
perform the implicit conversion of the context.

Where this is interesting for shared, is that I will write the
appropriate machinery to assure a safe calling environment, and then
cast the shared explicitly.
That is how you do any interactions with shared, and it's no different
here; you write unsafe code to confirm the proper environment, and
then cast shared. The library is written to implement and confirm
appropriate context.

> struct D {
>      int a;
>      void foo() shared {
>          pragma(msg, typeof(a)); // int   *** no, it is `shared(int)`
>      }
>
>      void bar() const {
>          pragma(msg, typeof(a)); // const int
>      }
> }
>
> So it is a bug for const at the very least, but shared is ya...

No, in your code above, the function receives a `shared(Context)*`,
and D qualifiers are transitive, so therefore the pragma should print
`shared(int)`, there is absolutely no magic here. Any attempt at magic
here is a terrible bug.
If you try and call foo(), you should get a compile error at the
callsite that it can't pass `Context*` to `shared(Context)*` which the
function receives. So you can't call a shared local function... but
that doesn't mean it's illegal to define one.
Like I say above, the value here is that you can implement machinery
to assert a valid calling environment and force the cast to make the
call.


More information about the Digitalmars-d mailing list