The lifetime of reduce's internal seed
monarch_dodra via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Apr 22 11:03:26 PDT 2014
On Tuesday, 22 April 2014 at 17:31:22 UTC, Ali Çehreli wrote:
> I opened the following bug before reading reduce's
> documentation carefully:
>
> https://issues.dlang.org/show_bug.cgi?id=12610
>
> import std.stdio;
> import std.algorithm;
>
> void main()
> {
> int[] arr = [ 0 ];
>
> int[1] seed;
> int[] result = reduce!((sum, _) => sum[])(seed, arr);
>
> writefln("%s", result);
> }
>
> The output is garbage like [373728176].
>
> Note that the original seed is a value type, which is later
> sliced by the lambda.
>
> Now I think that it can be considered a user error. Do you
> agree whit that? If so, this is something else we should be
> careful about similar to the internal buffer of byLine.
>
> Ali
I see one user bug, and one language bug, but no library issues.
The user bug is the lambda, that returns a reference to a local
("sum[]"). That said, I do believe the compiler should be able to
catch think.
The "int[] reduce...", however, I think is a outright language
issue. Implicilty calling opSlice on a static array is one thing,
but doing it on an rvalue is an outright aberration. The code
should be rejected no questions asked.
More information about the Digitalmars-d-learn
mailing list