March 26, 2014 Re: Problem with taking inout, const references | ||||
---|---|---|---|---|
| ||||
Posted in reply to Uranuz | On 03/26/2014 12:17 PM, Uranuz wrote: > 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; > } I think it is a bug then. I only now understand the problem. Here is a reduced case: class C { int[1] arr; inout(int)* foo() inout { foreach (ref e; arr) { static assert(is (typeof(e) == inout(const(int)))); // WAT? return &e; } } } void main() { (new C).foo(); } The problem is, when the foreach variable is ref, the type gains a const as well. Unless there is a reason for that we should file a bug. Another workaround for you is using the foreach index: { foreach(i, ref inout(Cookie) c; _cookies ) // NOTE i if( c.name == name ) return &_cookies[i]; // NOTE i return null; } > As I think compiler may be should create const version and mutable > version of this function. Type is a compile-time concept; there is no difference between the compiled mutable versus const versions of your inout function. So, there is no need for more than one compilation. > In case when it's mutable it should return pointer to mutable > pointer to *const* data when function is const. Makes sense and it should be checked at compile time. > I don't understand why compiler produce pointer to const, I think that's a bug. > In case when all is treated like const The body of an inout function must not mutate the object because the same body supports the non-mutable cases as well. So, it is acceptable for the compiler to compile as if it's const. However, as you say, the interface of the function still obeys the actual types used in the program. You are right. I think it is just a bug. Ali |
March 27, 2014 Re: Problem with taking inout, const references | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | > Type is a compile-time concept; there is no difference between the compiled mutable versus const versions of your inout function. So, there is no need for more than one compilation. Yes. It's my mistake. For CPU there is only data (that are sequence of bits) and comands (that are sequence of bits too). Type is human interpretation of meaning of these bits. > The problem is, when the foreach variable is ref, the type gains a const as well. Unless there is a reason for that we should file a bug. So problem is in foreach operator but not in *inout* itself. I haven't send a bug so I'm not familiar with it. How could I do it? |
March 27, 2014 Re: Problem with taking inout, const references | ||||
---|---|---|---|---|
| ||||
Posted in reply to Uranuz | On 03/26/2014 10:29 PM, Uranuz wrote: > I haven't > send a bug so I'm not familiar with it. How could I do it? Go to https://d.puremagic.com/issues/ Click "File an Issue" (but you may need to "Open a New Account" first). Product: D Component: DMD Version: D2 Importance: P3 normal A descriptive title... Additional Comments: the reduced code etc. Then click "Commit". Thank you, :) Ali |
March 27, 2014 Re: Problem with taking inout, const references | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | Posted bugreport. https://d.puremagic.com/issues/show_bug.cgi?id=12478 |
Copyright © 1999-2021 by the D Language Foundation