lua-users home
lua-l archive

Re: Aspect Oriented Programming

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


2011年1月3日 Gustavo de Sá Carvalho Honorato <gustavohonorato@gmail.com>:
> Hi,
>
> is there any easy way to work with Aspects in Lua? I've found this
> project (http://aspectlua.luaforge.net/) but it seems to be quite old.
as many things OO, most 'fun' things in aspects are trivial to do in
functional programming:
for example, it's common to enhance the built-on type() function:
do
 local oldtype = type
 function type(x)
 local mt = getmetatable(x)
 if mt and mt.__type then return mt.__type end
 return oldtype(x)
 end
end
or you could do some 'prepend' functions:
function prepend(f,pre)
 return function(...)
 return pre(f,...)
 end
end
and use like this:
type = prepend (type, function(old, x)
 local mt = getmetatable(x)
 if mt and mt.__type then return mt.__type end
 return old(x)
end
-- 
Javier

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