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

Timon Gehr via Digitalmars-d digitalmars-d at puremagic.com
Sun Jul 2 06:33:29 PDT 2017


On 02.07.2017 10:55, Walter Bright wrote:
> Neither I nor anyone here seems to understand its purpose.

The opposite is true. I understand it, and you seem to understand it 
partially:

On 02.07.2017 05:55, Walter Bright wrote:
> On 7/1/2017 6:22 PM, Stefan Koch wrote:
>> I cannot think so a single time I ever used const inout.
> 
> The math needs to work whether it is ever used or not, otherwise we wind 
> up with bizarre, intractable absurdities.

> 
> It's my fault as I should have noticed this getting slipped into the compiler. 

No, it is Kenji Hara's and my achievement. This is one of the things 
Kenji slipped into the language that actually should be there. If you 
have 'inout' there is no way around 'const inout'.

> The existence of it is likely a significant contributor to peoples' 
> confusion about inout.

The opposite is true. Many people are confused about inout and by 
extension about const inout.

The best way to think about inout is that it enables a function to have 
three distinct signatures:

inout(int)[] foo(inout(int)[] arg);

"expands" to:

int[] foo(int[] arg);
immutable(int)[] foo(immutable(int)[] arg);
const(int)[] foo(const(int)[] arg);


const inout /does not change this in any way/:


const(inout(int))[] foo(inout(int)[] arg);

expands to:

const(int)[] foo(int[] arg);
const(immutable(int))[] foo(immutable(int)[] arg);
const(const(int))[] foo(const(int)[] arg);

It would be confusing if it worked any differently.


More information about the Digitalmars-d mailing list