lua-users home
lua-l archive

Re: OOP sortof

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


On Sat, 2011年01月01日 at 15:44 +0200, steve donovan wrote:
> A more dynamic solution could look like this:
> 
> local property = {width=true,height=true,text=true}
> 
> function Box.__newindex(self,prop,value)
> if property[prop] then
> local name = '_'..prop -- i.e. actual field is _width, etc
> if self[name] ~= value then
> self[name] = value
> self:update()
> end
> else
> error("not a property or method",2)
> end
> end
> 
Nice. Should the "self[name] = value" not better be a "rawset(self,
name, value)"? Also: how would an auto-wrapper for a getter look like?
</nk>

AltStyle によって変換されたページ (->オリジナル) /