How to break const

Timon Gehr timon.gehr at gmx.ch
Wed Jun 20 07:55:52 PDT 2012


On 06/20/2012 04:36 PM, deadalnix wrote:
> Le 20/06/2012 16:21, Timon Gehr a écrit :
>> On 06/20/2012 02:38 PM, deadalnix wrote:
>>> Le 20/06/2012 13:58, Timon Gehr a écrit :
>>>>>> Clarification: 'const' means 'const'. No other qualifiers.
>>>>>>
>>>>>> There is no 'const' in that example code. 'immutable' obviously
>>>>>> needs to
>>>>>> be transitive regardless of the particular interpretation of 'const'.
>>>>>>
>>>>> const means: maybe immutable.
>>>>
>>>> Or maybe mutable. Therefore, interpretation '2.'
>>>>
>>>>> Thus const needs to be transitive too.
>>>>
>>>> Wrong. This is the (A==>B) ==> (B==>A) fallacy, where
>>>>
>>>> A: 'const' is transitive
>>>> B: 'const' references cannot modify 'immutable' data
>>>>
>>>
>>> I understand the difference. It can change the legality of some calls,
>>> that is true (and probably it is better).
>>
>> None is strictly better than the other. Interpretation 2. gains some
>> flexibility in respect to interpretation 1., but it loses some 'const
>> pure' guarantees.
>>
>> It is a matter of how these two orthogonal concerns are weighted
>> against each other. (Ideally, there would be one distinct qualifier for
>> each of the interpretations. :o) )
>>
>>> But it doesn't change the need
>>> for a frame pointer's qualifier (mandatory to ensure immutability
>>> transitivity).
>>>
>>> To benefit of the extra freedom granted by B, what could be the casts
>>> safely allowed on the delegate ? I understand your point, but fail to
>>> find any way to make it work in practice.
>>
>> It works if implicit conversions from 'R delegate(A)' to
>> 'R delegate(A) const' are allowed, as well as looking up an
>> 'R delegate(A) in a const receiver object.
>
> What about conversion from/to immutable ?

Simple application of transitivity. This part is the same for 1./2.

'R delegate(A)' and 'R delegate(A) const' are not convertible to
'R delegate(A) immutable', but 'R delegate(A) immutable' is
convertible to 'R delegate(A)' and 'R delegate(A) const'.

'R delegate(A)' and 'R delegate(A) const' decay to
'R delegate(A) immutable' when stored in an immutable object.


Regarding 'shared':

delegates that only access shared data can be marked as having a
'shared' context pointer.

'R delegate(A)shared' can convert to 'R delegate(A)', but not vice versa.

'R delegate(A)shared' can convert to 'shared(R delegate(A))', and vice 
versa.

In essence, for delegates, the operation of qualifying the 'tail' of
the delegate will be qualifying the context pointer.

eg:

static assert(is(
     typeof(cast()shared(R delegate(A)).init) == R delegate(A)shared
));





More information about the Digitalmars-d mailing list