0

Pretty confusing, I read this from Edition 1:

Technically speaking, what's a value in Lua is a closure, not the function. The function itself is just a prototype for closures.

and

Closure is a function plus all it needs to access non-local variables correctly

All it needs?, but what does it need to access non-local variables correctly? I can't really understand this at all. Are closures any different from functions ?

BlaztOne
  • 180
  • 1
  • 10
  • 1
    "*what does it need to access non-local variables correctly*" What does it matter? Whatever it is that it needs, it has. Think of it like a black box; you aren't really supposed to care what goes into it; what matters is how it *behaves*. A function can access non-local variables; that's its behavior. How it accomplishes that doesn't matter; you can pass the value of a closure around to anywhere and it will still work. – Nicol Bolas Aug 03 '20 at 13:48
  • I know it's late to ask this but your explanation on the "Implementation of closures in Lua?" question seems confusing: *"Since it is within a function, it has the right to access local variables declared outside of that function. "* What is **it**? the "b" function? "inner" variable? – BlaztOne Aug 05 '20 at 13:48
  • so ```local inner = 'asdf'``` is an upvalue right? or ```inner = 10```? – BlaztOne Aug 05 '20 at 14:00
  • `inner` is just a local variable. It is the access to `inner` which is or is not done as an upvalue, not `inner` itself. Accessing a `local` variable from a function other than the scope of the function that declared it is done through upvalues. – Nicol Bolas Aug 05 '20 at 14:56

0 Answers0