[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Is this possible? (garbage collect and weak tables)
- From: Roberto Ierusalimschy <roberto@...>
- Date: Fri, 6 Nov 2009 11:06:23 -0200
> 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
The correctness of this code rests on the assumption that the GC will
not run between the two assignments. It seems better style the other way
around:
local temp = translators[key] or {}
translators[key] = temp
... (use 'temp' for the rest of this code) ...
(Actually the assumption may be wrong! a line hook may cause a GC between
the two lines when debugging this code ;)
-- Roberto