lua-users home
lua-l archive

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


Ignacio Burgueño wrote:
Would this be safe?
local user = inconcert.authUser or {languageId=14346, codepage="UTF-8"}

local temp = translators[key] or {} -- get a strong reference to the
-- elements in the weak table
translators[key] = temp
local translator = temp[user.languageId]
if not translator then
translator = i18nlib.Translator{(arguments omitted)}
temp[user.languageId] = translator
end
return translator

That'd be safe, but I think you accidentally commented out a required line? (Indicated).

You could even just add this one line:

mt.__index = function(tab, key)
   local user = inconcert.authUser or {languageId=14346, codepage="UTF-8"}
   translators[key] = translators[key] or {}
--> local temp = translators[key] -- keep a strong reference to the table for this call
   local translator = translators[key][user.languageId]
   if not translator then
       translator = i18nlib.Translator{(arguments omitted)}
       translators[key][user.languageId] = translator
   end
   return translator
end

I wonder if LuaJit 2.x would incorrectly optimise the temp variable away =/? I'm guessing not, as the gc only runs in interpreted mode, but still interesting.

- Alex