returning 'ref inout(T)' - not an lvalue?

bitwise via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jan 25 13:44:32 PST 2017


On Wednesday, 25 January 2017 at 21:04:50 UTC, Adam D. Ruppe 
wrote:
> On Wednesday, 25 January 2017 at 20:42:52 UTC, bitwise wrote:
>> Is it not possible to return a ref from an inout function?
>
> It isn't the inout that's getting you, it is the const object 
> in main().
>
>     const(List!int) c;
>
> Make that mutable and it works. Why? Cuz the `C list` in the 
> iterator keeps that const with it.... I'm not sure why that 
> kills it though, the error tells me it is an internal cast that 
> is breaking things but I don't see why that logically would.

This was intentional, because I thought that due to transitivity, 
'alias T' would also be const, and opIndex would return const(T) 
when created from a const(List!T).

I just tried the following though, and it outputs 'int' rather 
than 'const(T)' for a const(List!T):

alias T = typeof(list.data[0]);
pragma(msg, T.stringof);

This is not what I thought would happen.

I actually changed it up like this, and things seem to work:

alias T = CopyTypeQualifiers!(C, typeof(list.data[0]));

ref inout(T) opIndex(int i) inout{
     return list.data[pos + i];
}

> This might arguably be a bug, but you could work around it by 
> checking for that const This and offering a different method 
> that just returns const instead of ref.

Given what the solution was, I think the error message could be 
improved, but I'm not sure what the right approach would be.

>>     T[] data = new T[1];
>
> BTW what do you think this line does? I ask because most people 
> who use it don't get what they expect out of it....

Should be an array of 'T' with all elements set to T.init right?
This was just an example - for some reason, I thought it would 
make things clearer.

   Thanks



More information about the Digitalmars-d-learn mailing list