lua-users home
lua-l archive

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

> > see it implemented in Lua (the basic types support).
> Just to remind you, Lua 4.0 already had support for this kind 
> of OO programming with basic types. For instance,
>   settagmethod(tag(""), "gettable", function (a,b)
>     return getglobal('str'..b)
>   end)
> >   ansiStr = "Hello"
> >   print(ansiStr:lower())
> Edgar gave several important contributions to Lua, but "basic 
> types support" was not one of them...

Edgar did it cleanly with a single metatable specifically made for the
basic type.  Using this type of support, I'm able to:

ansiStr = "Hello"
print(ansiStr:gsub(...whatever...))  -- Calls 'gsub' behind your back.
wideStr = L"Hello"
print(wideStr:gsub(...whatever...))  -- Calls 'ugsub' behind your back.

The tag method above would blow up on any function that didn't have
'str' in front of it.  The built-in 'gsub' does not.  Neither does
'concat' or 'format'.

Additionally, I'd have to extend to the tag method above to test first
for a string type, then do your code.  If that fails, I'd have to test
for a Unicode string type, then do the equivalent 'ustr*' function call.
Each time, it gets messier and messier.

Edgar's single metatable per basic type is a great, clean solution to
the problem.