Why do "const inout" and "const inout shared" exist?

Timon Gehr via Digitalmars-d digitalmars-d at puremagic.com
Sat Jul 1 21:49:39 PDT 2017


On 02.07.2017 06:45, Walter Bright wrote:
> On 7/1/2017 9:12 PM, Timon Gehr wrote:
>> On 02.07.2017 05:13, Walter Bright wrote:
>>> On 7/1/2017 3:12 PM, Timon Gehr wrote:
>>>> const(const(T))     = const(T)
>>>> const(immutable(T)) = immutable(T)
>>>> const(inout(T))     = ?
>>>>
>>>> It used to be the case that const(inout(T)) = const(T), but this is 
>>>> wrong, because if we replace 'inout' by 'immutable', the result 
>>>> should be immutable(T), not const(T). Hence const(inout(T)) cannot 
>>>> be reduced further.
>>>
>>> If const(inout(T)) is reduced to inout(T), it works.
>>
>> Counterexample:
>>
>> const(inout(char))[] foo(bool condition, inout(char)[] chars){
>>      if(!condition) return "condition failed!";
>>      return chars;
>> }
>>
>> Turn const(inout(char)) into inout(char) and the example no longer 
>> compiles. (Nor should it.)
> 
> I don't think that matters. There's no reason to write const(inout) for 
> a return value.


I think the example demonstrate the reason. It either returns the 
argument or an immutable global. If the argument is immutable, so is the 
return value, otherwise the return value is const.


More information about the Digitalmars-d mailing list