What functions can be called on a shared struct that's implicitly castable to immutable?

Simen Kjærås simen.kjaras at gmail.com
Mon Nov 4 01:34:26 PST 2013


On 04.11.2013 09:46, deadalnix wrote:
> You are trying to solve a non problem here. s is not shared in the first
> place, so you can start by fixing it here.
>
> Now, if S has some indirection, then it make sense to mark it as shared,
> but then the trick you propose won't work.

I believe we're talking past each other here. Perhaps a better example:

module foo;

struct S {
    immutable(int)[] arr;
    void fuzz() const pure {
    }
}

void bar(S s) {
    s.fuzz();
}

void main() {
    shared S* s = new shared S;
    bar(*s);   // a
    s.fuzz(); // b
}


This example demonstrates the exact same behavior as before, and again 
I'm not doing a lot of anything special to get line a to compile.

Now, I'm still of the opinion that the 'trick' I propose works - making 
a copy of s is cheap (as that's what the spec says about copying 
structs), s is implicitly castable to immutable, and the function to be 
called is guaranteed not to change s in any way. From all that, it seems 
safe to call fuzz on a shared S.


More information about the Digitalmars-d mailing list