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.

user1095108
  • 14,119
  • 9
  • 58
  • 116
  • 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](http://sscce.org). – lhf Apr 04 '15 at 18:17

1 Answers1

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.

user1095108
  • 14,119
  • 9
  • 58
  • 116
  • How is `a` a string if you have `a.key = b`? – Yu Hao Apr 04 '15 at 19:05
  • @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. – user1095108 Apr 04 '15 at 19:55
  • Show what you mean in the code of your own question. No one can see `a` and `b` are strings from `a.key = b`. – Yu Hao Apr 04 '15 at 20:01
  • @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. – user1095108 Apr 04 '15 at 20:04