lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Nov 25, 2014 1:58 PM, "Kang Hu" <hukangustc@gmail.com> wrote:
> Exercise 2.6: Assume the following code:
> a = {};
> a.a = a
> 1. What would be the value of a.a.a.a ? Is any a in that sequence somehow
> different from the others?
> 2. Now, add the next line to the previous code:
> a.a.a.a = 3
> What would be the value of a.a.a.a now?

Actually this will result in an error.

If you set 'a.a.a.a = 3', then as others have already pointed out, you are setting 'a.a' to 3.

However, when you want to evaluate 'a.a.a.a' now, you get an error: attempt to index a number value (field 'a')

The reason is that after 'a.a', you get 3, and then you try to index it again with ["a"], and you can't usually index a number.