The following code fails:
void next(T)(ref T[] a) { assert(a.length); a = a[1 .. $]; }
void main()
{
auto a = [ 1, 2, 3 ];
a.next;
assert(a == [ 2, 3 ]);
}
If one replaces "auto" with "int[]", the code compiles and runs as expected. It looks like auto with literals tries too hard to keep it around as an rvalue.
This seems to be related to issue 2606.
Comment #1 by ary — 2009-01-23T17:47:05Z
The problem is that an array literal has a static array type by default. Wouldn't it be more user-friendly for it to be a dynamic array type?
Comment #2 by andrei — 2009-01-23T20:03:18Z
(In reply to comment #1)
> The problem is that an array literal has a static array type by default.
> Wouldn't it be more user-friendly for it to be a dynamic array type?
Oh, you are right. Walter did change the default type of literals to dynamic-length, but apparently only for strings.
Comment #3 by 2korden — 2009-01-24T00:53:33Z
Could you please post generated error message, too, so that other don't have to guess? :)
...
a.next; // Error: cast(int[])a is not an lvalue
...
Comment #4 by bearophile_hugs — 2010-10-29T09:47:02Z