Avoid gratuitous closure allocations

Jonathan M Davis newsgroup.d at jmdavisprog.com
Sat Sep 21 00:38:59 UTC 2019


On Friday, September 20, 2019 5:21:22 AM MDT Ali Çehreli via Digitalmars-d-
learn wrote:
> tl;dr Instead of returning an object that uses local state, return an
> object that uses member variables.

The other issue this helps with is problems related to having multiple
contexts. IIRC, without it, some predicates don't work due to the compiler
complaining about there being more than one context. However, I pretty much
always use static structs for ranges though, so I haven't run into the issue
recently enough to recall the exact details. In general though, I'd say that
it's best pratice to use static structs in functions and avoid needing local
context. Sometimes, it makes sense to do so, but in general, giving your
struct access to the local scope in the function is going to result in
closures being allocated when they could have easily been avoided.

- Jonathan M Davis






More information about the Digitalmars-d-learn mailing list