lua-users home
lua-l archive

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


On Wed, Apr 16, 2014 at 11:23 AM, Dirk Laurie <dirk.laurie@gmail.com> wrote:
2014-04-16 9:34 GMT+02:00 Aapo Talvensaari <aapo.talvensaari@gmail.com>:
> What was the main reason that the strings don't share the metatable (i.e.
> every string has a new { __index = string }, and not like every string has
> (the same) 'string' as a metatable? Also it is not possible to setmetatable
> to strings. Just being curious.

Lua strings are internalized. I.e. every time you have "my_long_string",
even if it is created by table.concat({"my","long","string"},"_"), it refers
to exactly the same string. It will be chaos if some string you have
just strung together has a metatable you did not know about.

What I meant was that:

1. getmetatable("") == getmetatable("") -- returns false
2. you can do getmetatable("") but you cannot do setmetatable("", {})
3. why isn't string defined like this:

string = { __index = { find = function(s, pattern, index, plain) end, ...}}

if it was, then:

1. getmetatable("") == getmetatable("") -- returns true

And why you can even call getmetatable on strings if you cannot setmetatable (shouldn't this be totally hidden then -> strings just have these methods defined in global string, and that's it - getmetatable("") returns nil).


Regards
Aapo