user1095108
user1095108

Reputation: 14603

lua metatable bug or feature?

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.

Upvotes: 2

Views: 234

Answers (1)

user1095108
user1095108

Reputation: 14603

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.

Upvotes: 2

Related Questions