lua-users home
lua-l archive

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


2017-09-27 7:38 GMT+02:00 hz . <abrash_han@hotmail.com>:
> hi,
>
> Is there a way to init table field as:
>
> tbl = {
>  a = ‘aaa’,
>  b = a,
> }
>
> where b is inited as ‘aaa’?
>
> Or what’s the most simple approach to do this.

Simplicity is in the eye of the beholder. How about this?

tbl = let [[ a='aaa'; b=a ]]

It requires that the function 'let' be pre-written. That's not quite so
simple, but once done you have it. You can have any Lua code
inside that does not depend on local variables.

let = function(def)
   local ans = {}
   def = load(def,nil,nil,ans)
   if def then
     def()
     return ans
  end
end

The above version is semi-paranoid: it returns nil if your code
fails to compile. If your code compiles but does not run, it raises
an error.