Problem with taking inout, const references

Ali Çehreli acehreli at yahoo.com
Tue Mar 25 13:18:50 PDT 2014


On 03/25/2014 12:20 PM, Uranuz wrote:

 > I have problem with understanding of work of modifiers const, inout,
 > immutable modifiers.

I am sure you know these as well, but here is my quick list:

const: "I shall not modify data"
immutable: "I demand immutable data"
inout: "Whatever the actual type qualifier is"

 > As I understand inout keyword is intended to consume const,
 > immutable and data without modifiers.

Yes. Further, the code is compiled as 'const' (because const is 
compatible with mutable, immutable, and const).

 > I don't understand what is the purpose of inout.

So that the following function works with any kind of int array:

inout(int)[] first_two(inout(int)[] arr)
{
     // (input validation omitted)
     return arr[0..2];
}

We don't have to write three overloads just for these qualifiers. (Yes, 
templates with constraints can be used as well.)

 > Error: cannot implicitly convert expression (&c) of type const(Cookie)*
 > to inout(Cookie)*

Your code compiles with the development branch of dmd. What version are 
you using?

Ali



More information about the Digitalmars-d-learn mailing list