[Issue 10926] Wrong expression printed when ternary operator used as lvalue

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Nov 12 20:12:18 PST 2013


https://d.puremagic.com/issues/show_bug.cgi?id=10926



--- Comment #6 from yebblies <yebblies at gmail.com> 2013-11-13 15:12:14 EST ---
(In reply to comment #5)
> (In reply to comment #4)
> 
> > Would this make any sense?
> > 
> > (cast(const(int)[])c) ~= 20;
> 
> 
> You are probably right, but it's quite surprising:
> 
> 
> void main() {
>     const(int)[] a, b;
>     int[] c, d;
>     (true ? a : b) ~= 10; // OK
>     (true ? c : d) ~= 20; // OK
>     (true ? a : c) ~= 30; // Error: cast(const(int)[])c is not an lvalue
>     if (true)
>         a ~= 40; // OK
>     else
>         c ~= 40; // OK
> }
> 
> 
> So if both are fully mutable, or both have const items then you can append to
> them, but if one of them is mutable and the other has const items, then it
> can't append and you have to use a regular if statement.
> 
> Thank you for your answer, I'll keep this issue closed.


For a more involved answer:

Appending modifies length, so it must be done on lvalues.

`b ? a : c` can be transformed into an lvalue like this:
`*(b ? &a : &c)`

But if the types are different, it would have to end up as
`*(b ? &a : cast(const(int)[]*)&c)`

Which would allow
`(b ? a : c) = a;`

And that could cause 'c' to point to the data in 'a', which could be immutable.

=======================================

This could be possible, if a new re-write was introduced:

b ? a : c op ...   ->   b ? (a op ...) : (c op ...)

But I'm not entirely sure we want that.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list