lua-users home
lua-l archive

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


On 28 June 2012 09:29, luciano de souza <luchyanus@gmail.com> wrote:
> Hello all,
>
> I wasn't acquainted with OOP strategies in Lua. What I know is that we
> have nice options like Classlib, Loop, LOS... and the raw handling of
> metatables.
>
> Supppose the following structure:
>
> -- [[
> class number
> method GetValue
> property value
> ]] --
>
> Now suppose the following function:
>
> function number:GetValue()
> return math.random(100)
> end
>
> I would like to do something like that:
>
> number = number()
> print(number.value)
>
> The reply could be 80, 35, 44 or any randomic value between 0 and 100.
>
> Using Classlib, LOOP or any other Lua library with OOP support, how
> can I do this?

Steve Donovan's Penlight library has a class implementation, and if
you inherit from class.properties it will set up the metatable so that
get_* and set_* are automatically called. It does have the unfortunate
side effect that getters are automatically set up for variables
starting with an underscore, which I find problematic because I often
use _variables as pseudo-private fields.

https://github.com/stevedonovan/Penlight/blob/master/lua/pl/class.lua#L151

    henk