[Issue 15582] Slice returned by opSlice() not accepted as lvalue

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Sun Jan 24 07:08:40 PST 2016


https://issues.dlang.org/show_bug.cgi?id=15582

Marc Schütz <schuetzm at gmx.net> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement

--- Comment #6 from Marc Schütz <schuetzm at gmx.net> ---
@H. S. Teoh:

You're right that opSlice() should actually be opIndex(), but this has nothing
to do with the problem (I guess we both agree on that).

I may indeed be misunderstanding things. Let me comment on some of your points,
maybe my stance becomes clearer.

> Secondly, in opSlice()'s implementation, the notation `data[]` means "take a
> slice of the static array `data`", IOW, "make a dynamic array that points to
> the elements of the static data `data`".

Right. To clarify, since this may be a cause of confusion, what I mean by
"slice" is exactly a "dynamic array".

> So what you end up with, is that the LHS of the assignment is a dynamic array,
> and you're trying to assign the scalar 10 to it. This is, of course, invalid.

This is where I used to disagree. In my understanding, the language should
behave as if dynamic arrays `T[]` had an operator `T opAssign(T value)` that
does element-wise assignment.

Now, looking at [1], the documentation indeed describes the expected behaviour
in terms of a "slice operator", thus supporting your point of view.

> If the line marked "array copying notation" is really the same thing as
> slicing, then the line "x = y" ought to overwrite the first two elements of a
> with the last two elements of b.

I'm aware of this. But in `x = y`, `x` is an lvalue, see my comment #4.

But in light of what the specification says, the current behaviour is indeed
consistent. It seems that my mental model was indeed different.

I still think that the current behaviour is suboptimal, but it's certainly not
a bug then. I'm turning this into an enhancement request; people can close it
if it isn't useful.

[1] http://dlang.org/spec/arrays.html#array-setting

--


More information about the Digitalmars-d-bugs mailing list