On 4/6/22 2:32 PM, Salih Dincer wrote:
On Wednesday, 6 April 2022 at 16:54:26 UTC, Steven Schveighoffer wrote:

This is almost equivalent, but it requires a lambda and an allocation. So I'm not sure what thing you are trying to do here.


**Source Code:**
https://forum.dlang.org/post/pxkhngxmqgiwwymmg...@forum.dlang.org

Actually, I wrote this before, with a few magic touches, I got what I wanted.

I'm not sure what your code there is trying to do exactly.

What I meant with my comment above is this:

With `arr.find!(someLambda)`, if the lambda is using data from outside the lambda, it needs a closure, which means it may (probably does) allocate your needed data into a GC heap block that will then become garbage after the function is gone.

With `arr.find(value)`, it searches the array for something that compares with the value. No allocation happens, and it's equivalent to what you would write in a for loop.

Especially for the code in question:

```d
arr.find(val); // like a for loop, comparing each element to val
arr.find!(v => v == val); // requires a context pointer for val, may allocate.
```

There is no difference functionally -- both perform exactly the same task, but one is just more expensive.

-Steve

Reply via email to