shared - i need it to be useful

Stanislav Blinov stanislav.blinov at gmail.com
Sun Oct 21 11:01:17 UTC 2018


On Sunday, 21 October 2018 at 09:58:18 UTC, Walter Bright wrote:
> On 10/20/2018 11:08 AM, Nicholas Wilson wrote:
>> You can if no-one else writes to it, which is the whole point 
>> of Manu's proposal. Perhaps it should be const shared instead 
>> of shared but still.
>
> There is no purpose whatsoever to data that can be neither read 
> nor written. Shared data is only useful if, at some point, it 
> is read/written, presumably by casting it to unshared in 
> @trusted code. As soon as that is done, you've got a data race 
> with the other existing unshared aliases.

Just a thought: if a hard requirement is made on `shared` data to 
be non-copyable, a @safe conversion could be guaranteed. But it 
can't be implicit either:

shared(T) share(T)(T value) if (!is(T == shared) && 
!isCopyable!T) {
     shared(T) result = move(value);
     return result;
}

struct ShareableData {
     @disable <postblit and/or copy ctor>; // Generated by 
compiler in presence of `shared` members and/or `shared` methods

     /* ... */
}

void sendToThread(T)(shared T* ptr) @safe;

void usage() @safe {
     int x;
     sendToThread(&x); // Error: 'x' is not shared
     shared y = x;     // Ok
     sendToThread(&y); // Ok


     ShareableData data;
     sendToThread(&data); // Error: 'data' is not shared
     auto p = &data;
     sendToThread(p);     // Error: *p is not shared

     auto sharedData = share(move(data));
     sendToThread(&sharedData); // Ok

     auto yCopy = y;   // Error: cannot copy 'shared' y
     auto dataCopy = sharedData; // Error: cannot copy 'shared' 
sharedData

     ShareableData otherData;
     sendToThread(cast(shared(ShareableData)*) &otherData); // 
Error non- at safe cast in @safe code
}

And again, we're back to 'once it's shared, it can't be @safe-ly 
unshared', which ruins the distinction between owned and shared 
references, which is one of the nicer properties that Manu's 
proposal seems to want to achieve :(


More information about the Digitalmars-d mailing list