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

Simen Kjærås simen.kjaras at gmail.com
Wed Nov 6 02:28:53 PST 2013


On 06.11.2013 04:59, deadalnix wrote:
> On Wednesday, 6 November 2013 at 03:07:08 UTC, Simen Kjærås wrote:
>> I cannot for the life of me see how my suggestion violates SC, and you
>> even seem to say it doesn't ("making a copy is fine"). Are you arguing
>> about something other than what I'm arguing about?
>
> Calling member function is a pass by reference. The reference is shared.
> SO you can't call a pure const method on a shared object.
>
> You can pass a shared object by value to a function expecting a const
> (as long as it do not contains indirection and is copyable).

Turned my brain back on again this morning, and things became more 
clear. The problem is in my claim that the type is convertable to 
immutable, which is not the case (since it ends up with a *reference to 
a mutable* array of immutable elements). Now, for the example I've asked 
for:

struct S {
     immutable(int)[] arr;

     int fuzz() const pure {
         while (arr.length) {}
         return 3;
     }
}

However, the problem here is not one of sequential consistency. In fact, 
this function could copy arr.length to a local variable and enter an 
infinite loop, and still not violate sequential consistency. This would 
of course be useless, but still valid program behavior.

--
   Simen



More information about the Digitalmars-d mailing list