I know temporaries become like static values, and globals become like references, but I'm not sure how class members behave yet.
I think by "temporaries" you mean non-static local variables. They become "like static values" because they retain their value between calls to the closure. But I think maybe you're overthinking it, or maybe not generalizing enough.
Closures capture variables by reference - you could say - regardless of whether they are local, global or static.
- If the variable is global, the closure has a reference to a global variable.
- If the variable is static, the closure has a reference to a static variable.
- If the variable is local, the closure has a reference to a local variable.
There's meant to be fundamentally no difference between these cases as far as the closure is concerned.
The closure just has a reference to a variable. Every time you call the same closure, it still has the same references, so they
all act like global or static variables or properties of the closure itself.
You might distinguish between local variables as
temporary and global or static variables as
persistent, but there's a better perspective: like objects, any variable exists for as long as someone has a reference to it.
By default, local variables are only referenced by their name until the function returns. Closures capture references to the outer function's local variables, keeping hold of them until the closure itself is freed.
You can also make more references to a local variable explicitly. For instance,
return &localVar returns the local variable by reference to the caller, allowing the caller to retrieve or assign it. Alternatively, your function could explicitly capture a variable by taking its reference and binding it to a parameter, like this:
((&localVar) => ...).Bind(&localVar). Now
localVar refers to the same variable in both the outer function and the inner function, but the inner function is a BoundFunc rather than a Closure.
Closures capture variables. "Class members" have no relevance to closures. Your closure captures
Chain, not
Chain.User.