lua-users home
lua-l archive

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


-----BEGIN PGP SIGNED MESSAGE-----
Hash: RIPEMD160

John Belmonte schrieb:

> Common ways in which such lists are created include:
> 
>     { 2, nil, 'bar' }  -- literal list with nil
>     { f() }            -- capture function return vals which include nil
>     { ... }            -- capture function/chunk args which include nil

The simplest solution I'm using, borrowed from FP, is:

  function const(...)
    local n = select("#", ...)
    local t = {...}
    return function() return unpack(t,1,n) end
  end


So const() creates a new function which returns the original arguments.

  foobar = const(1,2,3,nil,nil,nil)

  print( foobar() )


Optionally, const() implemented in C can use upvalues for small numbers
of arguments and a table for large numbers of arguments.


Michael Roth

-----BEGIN PGP SIGNATURE-----

iD8DBQFGuHXlSIrOxc3jOmoRAxl4AJ0d6krNM5VjWBMyMSZD41LpmooHeACgl/6J
gI3P0cW+SdVX00NyYGD3oSA=
=aFUm
-----END PGP SIGNATURE-----