lua-users home
lua-l archive

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


OK, so that's just brilliant, and was exactly what I was looking for.  Now the question is, what am I potentially getting myself into by overwriting the __tostring metamethod?

Just to give everyone a better idea of my code structure, I'm not using modules and all functions are in the global space, so getting the global metatable like below should work fine for me.

t={}
t["english1"]={"english1","spanish1","french1"}
t["english2"]={"english2","spanish2","french2"}
t["english3"]={"english3","spanish3","french3"}

language=2 -- Spanish

getmetatable("").__tostring=function(in_string)
    if t[in_string] ~= nil and t[in_string][language] ~= nil then
        return t[in_string][language]
    else
        return in_string
    end
end

print("english1")

a_value="english2"
print(a_value)

print("notintable")

Thanks again, I love this language and the fine people who support it.
Brett

On Dec 12, 2007 3:43 PM, Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br> wrote:
> You can't do that in pure Lua, because in some cases no metamethod is
> involved (see below).

sure there is. try this:

getmetatable("").__tostring=function (x) return "<<<"..x..">>>" end

for k,v in next,string do print(k,v) end
print("hello")