The lifetime of reduce's internal seed

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Apr 22 11:34:46 PDT 2014


On Tue, 22 Apr 2014 14:17:57 -0400, Ali Çehreli <acehreli at yahoo.com> wrote:

> I don't think there is slicing an rvalue though. (?) reduce() is taking  
> a copy of the seed and then returning a slice to it because the user  
> slices it in their lambda. It effectively does the following, which  
> unfortunately compiles:
>
> int[] foo()
> {
>      int[1] sum;
>      return sum[];    // <-- no warning
> }

It's not slicing an rvalue, but the above is trivially no different than:

int[] foo()
{
    int[1] sum;
    return sum;
}

which does NOT compile.

The issue is that D's escape analysis is very very simplistic. It would be  
nice if it were better.

Ironically, because of return type inference in lambdas, you circumvented  
the check!

-Steve


More information about the Digitalmars-d-learn mailing list