lua-users home
lua-l archive

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


as far as i remember all strings share one metatable, so if you change it all strings behavious will change. so better don't touch strings metatable...


On Wed, Feb 6, 2013 at 3:24 AM, TNHarris <telliamed@whoopdedo.org> wrote:
>From the manual:
     function concat_event (op1, op2)
       if (type(op1) == "string" or type(op1) == "number") and
          (type(op2) == "string" or type(op2) == "number") then
         return op1 .. op2  -- primitive string concatenation
       else
         -- call the metamethod
       end
     end

     function len_event (op)
       if type(op) == "string" then
         return strlen(op)      -- primitive string length
       else
         -- call the metamethod
       end
     end

So if I wanted to, say, return the character length of a UTF-8 string, I
couldn't use
     getmetatable("").__len = utf8length

What is the motivation for not allowing the __concat and __len metamethods to
be set on strings?

--
tom <telliamed@whoopdedo.org>