-
Notifications
You must be signed in to change notification settings - Fork 5
/
value-reference.lua
67 lines (54 loc) · 1.45 KB
/
value-reference.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
-- Return a table entry that is an integral-value.
local function foo(thing)
return thing.z.w
end
-- Construt a new reference-value based on a table entry.
local function bar(thing)
local z = { w = thing.z.w }
return z
end
-- Return a reference-value from a table.
local function baz(thing)
return thing.z
end
local a = { x=0, y=1, z={w=0} }
-- This will be an integral-value.
local z1 = foo(a)
-- This will be a new reference-value, a copy.
local z2 = bar(a)
-- This will be a reference-value into the table.
local z3 = baz(a)
-- This should still print 0 as z1 is an integral-value.
local z1 = 1
print("Should be 0: " .. a.z.w)
-- This should print 0 and then 2 as we're writing to the
-- copy. If this was a reference-value we'd expect the
-- same value.
z2.w = 2
print("Should be 0: " .. a.z.w)
print("Should be 2: " .. z2.w)
-- This should print 3 both times as we're modifying though
-- the reference-value.
z3.w = 3
print("Should be 3: " .. a.z.w)
print("Should be 3: " .. z3.w)
local function printkvps(l)
for k, v in pairs(l) do
print(k .. "=" .. v)
end
end
-- You might think this modifies the table, but it doesn't,
-- because pairs() returns an index and value tuple, not an
-- an index and value-reference tuple.
local b = { 0, 1, 2 }
for k,v in pairs(b) do
v = v + 1
-- a[k] = v + 1
end
printkvps(b)
-- Instead you need to manipulate the table directly via
-- the key generated from pairs.
for k,v in pairs(b) do
b[k] = v + 1
end
printkvps(b)