lua-users home
lua-l archive

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


On Wed, May 13, 2009 at 6:32 PM, Luiz Henrique de Figueiredo wrote:
>> Is there a way to write an expand function that make use of the local variables (not global)?
>> function do_stuff (p1) print(expand'$(p1+1)') end
>
> You could use the debug library for that, though its use for ordinary
> programming is not recommended. If you have control over do_stuff, then
> you could created a table with the values of the locals before calling
> expand and pass it to expand.

Using debug to access lexicals is demonstrated under "Using debug to
Access Lexicals" in [1].  It's indeed considered a hack.  Both [1] and
[2] illustrate the approach of passing values as function arguments,
which is hackish.

A number of times I've felt that there should be some language support
for putting variables in strings.  The basic problem occurs in a
number of places [1-5] .  The way I think it should work is that
writing this:

  f [[a %(b) c %(d) e]]

would be equivalent to

  f( [[a %(1) c %(2) e]], b, d)

It can be done in Metalua though, along the lines of the string
interpolation solution [6].  This is also probably simple enough to do
with a lighter weight solution like token filtering.

[1] http://lua-users.org/wiki/StringInterpolation
[2] http://lua-users.org/wiki/ShortAnonymousFunctions
[3] http://lua-users.org/wiki/ListComprehensions
[4] http://lua-users.org/wiki/TableScope (near ShortAnonymousFunctions )
[5] http://lua-users.org/wiki/VarExpand (templatizing)
[6] http://lua-users.org/wiki/MetaLuaRecipes