how to assign lua variable by reference -
how can assign variable reference in lua one?
for example: want equivalent of "a = b" pointer b
background: have case have this:
local a,b,c,d,e,f,g -- lots of variables if answer == 1 -- stuff elsif answer == 1 -- stuff b . . .
ps. example in below appears apparent b=a value. note: i'm using corona sdk.
a = 1 b = a = 2 print ("a/b:", a, b) -- output: a/b: 2 1
edit: regarding clarifed post , example, there no such thing type of reference want in lua. want variable refer variable. in lua, variables names values. that's it.
the following works because b = a
leaves both a
, b
referring same table value:
a = { value = "testing 1,2,3" } b = -- b , refer same table print(a.value) -- testing 1,2,3 print(b.value) -- testing 1,2,3 = { value = "duck" } -- refers different table; b unaffected print(a.value) -- duck print(b.value) -- testing 1,2,3
you can think of variable assignments in lua reference.
this technically true of tables, functions, coroutines, , strings. may well true of numbers, booleans, , nil, because these immutable types, far program concerned, there's no difference.
for example:
t = {} b = true s = "testing 1,2,3" f = function() end t2 = t -- t2 refers same table t2.foo = "donut" print(t.foo) -- donut s2 = s -- s2 refers same string s f2 = f -- f2 refers same function f b2 = b -- b2 contains copy of b's value, since it's immutable there's no practical difference -- on , forth --
short version: has practical implications mutable types, in lua userdata , table. in both cases, assignment copying reference, not value (i.e. not clone or copy of object, pointer assignment).
Comments
Post a Comment