dictionary - How would you index a table that is being initialized? -
an example of desire:
local x = {["alpha"] = 5, ["beta"] = this.alpha+3} print(x.beta) --> error: [string "stdin"]:1: attempt index global 'this' (a nil value)
is there way working, or substitute can use without code bloat(i want presentable, fenv hacks out of picture)
if wants take crack @ lua, repl.it testing webpage quick scripts
no there no way because table not yet exist , there no notion of "self" in lua (except via syntactic sugar table methods). have in 2 steps:
local x = {["alpha"] = 5} x["beta"] = x.alpha+3
note need square brackets if key not string or if string characters other of [a-z][a-z][0-9]_
.
local x = {alpha = 5} x.beta = x.alpha+3
update:
based on saw on pastebin, should differently:
local alpha = 5 local x = { alpha = alpha, beta = alpha+3, gamma = somefunction(alpha), eta = alpha:method() }
(obviously alpha has no method because in example number idea, wanted show if alpha object).
Comments
Post a Comment