lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

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())

> local a; local function x() local b return function() return b end end
> print(x()==x())

For implementation details see

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