Hi,
In the book "Programming in Lua, 4th edition", there's a question about tables. It's got me a little confused. Here it is:
My understanding of `a.a = a` is that now table `a` has a member called `a` which is a reference to the table itself. I think of references in Lua as behaving like dereferenced pointers. So we effectively have a member that points to the structure itself, and that's my answer to part (a).
It doesn't matter if we do `a.a.a.a.a.a.a.a.a` and so on, this is just the structure itself. So my answer to part (b) is "No."
I'm not sure about part (c) though. Would be grateful for some help. (And please correct my answers to (a) and (b) if I'm wrong about them...)
Thanks.
In the book "Programming in Lua, 4th edition", there's a question about tables. It's got me a little confused. Here it is:
Assume the following code:
a = {}
a.a = a
(a) What would be the value of a.a.a.a?
(b) Is any a in that sequence somehow different from the others?
Now, add the next line to the previous code:
a.a.a.a = 3
(c) What would be the value of a.a.a.a now?
My understanding of `a.a = a` is that now table `a` has a member called `a` which is a reference to the table itself. I think of references in Lua as behaving like dereferenced pointers. So we effectively have a member that points to the structure itself, and that's my answer to part (a).
It doesn't matter if we do `a.a.a.a.a.a.a.a.a` and so on, this is just the structure itself. So my answer to part (b) is "No."
I'm not sure about part (c) though. Would be grateful for some help. (And please correct my answers to (a) and (b) if I'm wrong about them...)
Thanks.