Problem with taking inout, const references
Uranuz
neuranuz at gmail.com
Wed Mar 26 12:17:55 PDT 2014
I modified this with casting pointer to pointer to *inout* and it
compiles then.
inout(Cookie)* opBinaryRight(string op)(string name) inout if(op
== "in")
{ foreach( ref inout(Cookie) c; _cookies )
if( c.name == name )
return cast(inout(Cookie)*) &c; //Error is here
return null;
}
As I think compiler may be should create const version and
mutable version of this function. In case when it's mutable it
should return pointer to mutable pointer to *const* data when
function is const. In first case it will be able to modify
variable from outside using this pointer to mutable. Otherwise if
pointer to *const* produced modification is not allowed. I don't
understand why compiler produce pointer to const, when inout
"converts" to mutable. I think that compiler should check that
object field is not modified inside it's const method, but I
don't understand why it produces pointer to nonconst in case when
object field shouldn't be const.
In case when all is treated like const I don't understand how
*inout* could be useful to get mutable references and pointers as
result of *inout* method when object (and it's fields) are
mutable. Is this behaviour forbidden for some reason that I
don't know?
More information about the Digitalmars-d-learn
mailing list