On 08/18/2016 12:25 AM, Ali Çehreli wrote:
> I'm wondering whether there is such a thing as single-use of a type in
> compiler technology. I think if the compiler could determine that a type
> is used only once, it could apply optimizations.
> 
> A colleague of mine raised the issue of D's use of the GC even for
> seemingly local delegates. For example, even though everything remains
> local for the following lambda, countAbove() cannot be @nogc:
> 
> auto countAbove(int[] a, int limit) {
>     return a.filter!(x => x >= limit).count();
> }
> 
> The reason is due to the fact that filter() returns a struct object that
> takes the delegate as an alias template parameter. Here is a reduction
> of the issue with my understanding in comments:

I believe actual reason is that aliased lambda has to allocate a closure
because it refers to stack local variable (limit). This compiles just fine:

auto countAbove(int[] a, int limit) @nogc {
    return a.filter!(x => x >= 1).count();
}

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to