lua-users home
lua-l archive

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


I wrote:

> For instance, in the following code
> 
>   f = function() return function(s) print(s) end end
>   print f() "foo"
> 
> f gets turned into a function, but it doesn't immediately
> get called with the argument "foo".  Instead there's a parse
> error.  So obviously there's something I'm missing.

Reading that again, I'm thinking that f never gets turned
into a function.  Anyway, Matt wrote:

> There's an extra "print"... Just doing:
> 
>   f = function() return function(s) print(s) end end
>   f() "foo"
> 
> works just fine...

But that prints "foo", rather than printing the return value
of the function returned by f (namely nil).

I only came up with that example, because I didn't
understand how the

foo = -L "line 1" "line 2" "line 3" 

code worked.  I think I've got it now, though.  The
preceding line of code had a certain behaviour that I
couldn't duplicate, but now I've duplicated it with:

function f(x)
  if x then
    print(x)
    return f
  end -- if
end -- f
foo = f "bar" "baz" "qux"

which prints "bar", "baz", and "qux" (and assigns f to foo).

-- 
Aaron