2

If I have this construct:

a.key = b

and both a has a metatable attached and b has a metatable attached. Then b's metatable setter will be called to set key to b. Is this a bug of lua 5.3.0?

EDIT: a and b are strings.

1
  • 2
    It seems that a and b are sharing a metatable or you're misinterpreting the arguments in the newindex handler. You need to provide an SSCCE. Commented Apr 4, 2015 at 18:17

1 Answer 1

2

Tables and full userdata have individual metatables (although multiple tables and userdata can share their metatables). Values of all other types share one single metatable per type; that is, there is one single metatable for all numbers, one for all strings, etc. By default, a value has no metatable, but the string library sets a metatable for the string type (see §6.4).

Answer from the docs. It is a feature: a and b were string and hence shared their metatable.

Sign up to request clarification or add additional context in comments.

4 Comments

How is a a string if you have a.key = b?
@YuHao a metatable can be set on any value, just that it may not be an individual metatable. In case of strings it is shared among all the strings. Hence, a string may have properties and other meta-features.
Show what you mean in the code of your own question. No one can see a and b are strings from a.key = b.
@YuHao it was done via the C API, hence I couldn't paste the code. There would be too much of it. Nor did I indeed do a.key = b, the equivalent of that was done via the C API. I'll revise my question and state that a and b were strings.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.