lua-users home
lua-l archive

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


paul@theV.net <paul@theV.net> wrote:
> I have the following code:
> 
...
> 
> But Lua says
> 
> error: cannot access a variable in outer scope;
>   last token read: `f' at line 7 in file `a.lua'

You need to use "upvalues" (see the manual), e.g.

add = function (x, y)
        return x + y
    end

partial = function (f, i)
        local c = function (j)
                        return %f(%i, j)
                  end
        return c
    end

add_one = partial(add, 1)

print (add_one(2))

-- 
CINEMUCK (SI ne muk), n.  The combination of popcorn, soda, and melted
chocolate which covers the floors of movie theaters.
	-- Rich Hall, "Sniglets"