[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Why are these functions not equal?
- From: katlogic@...
- Date: Sat, 29 Mar 2014 10:38:53 +0100
On Sat, Mar 29, 2014 at 09:17:49AM +0200, Dirk Laurie wrote:
> The Lua 5.2 manual says:
> > x=function() end; y=function() end
> > print(x==y)
> false
> > print(string.dump(x)==string.dump(y))
Those are two different prototypes. Functions are not interned
by contents like string. The "sameness" is result of closure
cached in same function prototype.
Prototype literally means 'function' keyword in lua, writing it
twice will be always two different functions.
The cache in prototype checks if upvalues are same. If so, it will
return the cache entry. The latter example fails because upvalue
is different (b is instantiated on call to x).
To demonstrate:
> local a; local function x() return function() return end end
> print(x()==x())
true
but
> local a; local function x() local b return function() return b end end
> print(x()==x())
false
For implementation details see
http://www.lua.org/source/5.2/lvm.c.html#getcached
I suppose the manual is intentionally vague (same function prototype
may or may not return duplicate closures) to leave room for alternative
implementations (hoisting the closure to the level of nearest upvalue
is a bit more efficient than caching, but semantics of sameness will be
different).