lua-users home
lua-l archive

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


On 29 April 2011 19:09, uki <ukaszg@gmail.com> wrote:
> local _M = {}
> -- stolen from: https://github.com/kikito/middleclass/wiki/Private-stuff
> local _private = setmetatable({}, {__mode = "k"})
>
> --constructor
> function _M:new(...)
>   local object = {}
>    -- do stuff
>
>   _private[object].privateField = 1
>   return object
> end

Just beware of memory leaks pre-5.2 if the value contains a reference
to the object.

    henk