Reuse of Function

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Reuse of Function

Dong Feng
I read the following in Lua 5.2 References:

"Equality between function values has changed. Now, a function
definition may not create a new value; it may reuse some previous
value if there is no observable difference to the new function."

What I think, but unsure about, is here "function" actually means
"closure". The most plausible, to me, of reusing a "function
definition" (actually a closure, if I understand correctly) is a
enclosing function (or loop, or other blocks) of a closure is invoked
more than once. In terms of code, I guess it means Closure is reused
rather than Proto.

I'm not sure what "observable difference" means. I don't think it
means two function definitions appearing in different places of source
code with almost or exactly the same code string will generate one
single shared Proto struct.

Is my understanding correct?

Thanks,
Dong

Reply | Threaded
Open this post in threaded view
|

Re: Reuse of Function

Roberto Ierusalimschy
> What I think, but unsure about, is here "function" actually means
> "closure".

Yes; see the original description:

  Closures with the same reference are always equal. Closures with any
  detectable difference (different behavior, different definition) are
  always different.

(Yes, we know that "same reference" is not very clear...)


> I'm not sure what "observable difference" means.

It means that the result of some expression can change if you replace
one function by the other.


> I don't think it means two function definitions appearing in different
> places of source code with almost or exactly the same code string will
> generate one single shared Proto struct.

Two function definitions f1 and f2 appearing in different lines of
source code have an observable difference: debug.getinfo(f1).linedefined
is diferent from debug.getinfo(f2).linedefined. So, Lua must consider f1
different from f2.

-- Roberto