lua-users home
lua-l archive

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


On Wed, Apr 16, 2014 at 4:40 PM, Aapo Talvensaari <aapo.talvensaari@gmail.com> wrote:
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).

My bad, getmetatable("") == getmetatable("") returns true. What I was thinking... I'm almost sure that I tested this, and it didn't work, but I was wrong. Sorry.

It works indeed like I was suggesting. Forget what I just said.