Advertisement

Lua : Pointers / Reference Variables

Started by July 21, 2003 11:14 PM
14 comments, last by Ironica 21 years, 4 months ago
Hey, hehe. Probably another dumb question, but I can''t seem to figure this out from the manual. I just wondered, how can you create a reference variable in Lua? I been playing around...

function r(v)
 return v
end

a = r
b = r(1)
c = r("Hello")
print(type(a)) -- function
print(type(b)) -- number
print(type(c)) -- string
 
Right, so a is a reference variable, b and c both hold the return value of r, right? Or is the function body just copied to a? Ok well anyway, that''s simple enough. But how about something like:

function p(x)
 x = x + 1
end
d = 1
p(d)
print(d) -- 1
 
But I want p to change the value of d, not copy it to x. How do I do that?
function p(x)   return x + 1endd = 1d = p(d)print(d) -- 2  


It might be because your example isn't representative, but avoid modifying by reference if you can. It just makes things more complicated. This is true in C/C++ too, but in that language you sometimes don't have a choice (i.e. multiple return values). Lua can return multiple values which is a much cleaner mechanism.

If you really want references, you could use a table.


[edited by - JuNC on July 22, 2003 6:20:48 AM]
Advertisement
Yeah ok, I suppose that would be just as good. But how about this, which is quite similar to my problem...
Map_1 = createmap()Monster_1 = createmonster()Monster_1.map = Map_1 

I don''t want a copy of Map_1, I just want to be able to access it from Monster_1.
If createmap() returns a table then thats what will happen. Tables are only ever referenced, never copied by assignment.

a = { 4, 4,4 }

b = a -- b is a reference to the table, which a also references

function get_a()
return a
end

c = get_a() -- c is another reference to that table



To sum up what other posters have alluded to: tables, userdata, strings, and threads are always passed by reference. All other datatypes are passed by value.

How appropriate. You fight like a cow.
(which is spread out throughout the manual so it''s not surprising it can be confusing to work out which are which)

Interestingly, the 5.0 manual has:

quote:
Tables, functions, and userdata values are objects: variables do not actually contain these values,
only references to them.


So it doesn''t explicitly mention threads or strings, I don''t think strings can be modified in place (in which case, reference or not is basically irrelevant), but I haven''t used threads in Lua so I can''t comment.
Advertisement
Right. Basically, anything that isn''t passed by reference is immutable, so it might as well be passed by reference.
Sneftel> Just so you know, I wasn''t trying to prove you wrong, I checked the manual to find out what it said about functions, I''d have said (as the manual does) that functions are values and would be passed around like that, but the manual then also says they are objects and thus referenced. I think there is a slight skewing of terms in the manual - after all, why should functions (immutable) be passed by ''reference'' when strings (also immutable) aren''t? We know they''re both values so wha''th''ell is goin on
I agree that the manual is confusing. One factor in this is how strings in Lua are stored; there''s a global hash-table. So if you do

a = "foo"
b = "foo"

Then a and b actually point to the same location. The terminology is thus confusing, but the behavior is not; just treat every variable as a reference, and every datum as an object.

quote: Just so you know, I wasn''t trying to prove you wrong
You did, tho. I''d forgotten about functions in my list of referenced datatypes. It''s only since Lua 5 that functions were mutable, tho; up until then, upvalues were constants, so there was no ability to change a function''s state.

How appropriate. You fight like a cow.
Yeah, the actual behaviours work ''about right'', the semantics match to what you''d expect even if the manual doesn''t quite

I didn''t realize functions are mutable now, in that case it does make more sense, although TBH I''d prefer if they weren''t. IMO mutability is a big contributor to hard to understand code which Lua shouldn''t succumb to. But then again, it''s not exactly forced since I treat functions as immutable in 5 and it''s never bitten me.

This topic is closed to new replies.

Advertisement