`shared`...

Jonathan M Davis newsgroup.d at jmdavisprog.com
Mon Oct 1 10:50:38 UTC 2018


On Monday, October 1, 2018 3:55:41 AM MDT ag0aep6g via Digitalmars-d wrote:
> On 10/01/2018 08:47 AM, Nicholas Wilson wrote:
> > In order to be safe, a mutable parameter can be implicitly cast to
> > shared iff the parameter is also scope (that includes the `this`
> > reference`). With an implicit cast in place of the explicit cast under
> > the new rules it would fail to compile because the `this` reference is
> > not scope.
>
> I don't see why it would fail to compile. There's no reason why my
> `doThing` couldn't be marked as `scope`. It doesn't leak anything.
>
> `pure` would break the example. I'm not sure if it would ensure safety,
> though. Can a `pure` method spawn a new thread (that outlives the method
> call)?

pure is not sufficient regardless of what happens with threads, because it
also has to be proven that no reference can escape through any of the
parameters or the return value. E.G. depending on the parameters, it could
actually be possible to assign to a module-level variable inside a pure
function via a pointer that was a member variable of one of the parameters.
Depending on how much information the compiler has about the types involved,
_maybe_ it could prove it without scope if pure is involved, but it gets
pretty thorny. Certainly, it's way, way simply just to use scope and force
the programmer to continue to cast in those cases that the compiler can't
prove correctness just like we have to do now.

- Jonathan M Davis





More information about the Digitalmars-d mailing list