lua-users home
lua-l archive

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




On 03/10/16 07:43 AM, Mike Jones wrote:
Hi,

I am working on a project where I have basically an event style
framework, and I would like some way to "chain together" functions. I
have had a quick look at OO libraries for Lua but they don't seem to
be suited to what I am trying to do. I can't seem to find an example
that fits my use case of wanting parrallel modules providing the same
functions.

Here is a short example of what I am doing at the moment:

-- core function, defined in common header
function dosomething(arg)
     print("Orginal dosomething()")
    return 0
end

-- module 1 included in script, adds extra code on to existing function
pre_module1_dosomething = dosomething
function dosomething(arg)
     print("Module 1 dosomething()")
     return pre_module1_dosomething(arg)
end
dosomething = (function(dosomething) return function(arg)
  print "module 1 dosomething()"
  return dosomething(arg)
end end)(dosomething)

A syntax sugar for the above would be nice, yes. If we had lambdas using "->" syntax, then:

dosomething = (dosomething -> function(arg)
  print "module 1 dosomething()"
  return dosomething(arg)
end)(dosomething)

In my example the functions are in different files, but that's the
basic pattern I am currently using to chain extra code on to the end
of a function that has already been defined. When executed it should
output "Module 1 dosomething()" followed by "Original dosomething()",
ie both functions get executed. Is there a better way of achieving
this sort of functionality? the use of the "pre_module1_dosomething"
variable to hold the parent function seems very ugly to me.

- Mike Jones


--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.