lua-users home
lua-l archive

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


Aaron Brown <aaron-lua <at> oakadaptive.com> writes:

> 
> Martin Kendall wrote:
> 
> > I am looking at extending the multiple inheritance example
> > in "Programming In Lua" (ch. 16.3).
> 
> > [...] it is mixed into the execution of the current method
> > (i.e. executed in the same scope and then execution of the
> > original method continues).
> 
> Deciding at runtime what scope (including locals, not just
> globals) to run something in would require use of the debug
> library (or maybe it could be done with a macro system).
> 
> I may not have fully understood what you're trying to do, in
> which case my comment may not apply.
> 


Thank you for your response.  I realised as I sent the email that my comment 
about scope was misleading.  Perhaps giving an example would help (taken from 
the XOTCL tutorial):

#Define SoccerTeam class with transferPlayer method
Class SoccerTeam 
SoccerTeam instproc transferPlayer {playername destinationTeam} {
    foreach player [my info children] {
      if {[$player istype Player] && [$player name] == $playername} {
        $player move [set destinationTeam]::[$destinationTeam autoname player%
02d]
      }
    }
  }

#Define TransferObserver class with transferPlayer method
Class TransferObserver
TransferObserver instproc transferPlayer {pname destinationTeam} {
    puts "Player '$pname' is transfered to Team '[$destinationTeam name]'"
    next
}

#Mixin the TransferObserver behaviour into SoccerTeam 
#So SoccerTeam now has two transferPlayer  methods (and mixins take higher 
precedence, so that will be called in preference to the original
SoccerTeam instmixin TransferObserver

With this setup performing:
bayernMunich transferPlayer "Giovanne Elber" chelsea

Would cause the transfer message to be output, then the original transfer 
function would be exceuted when "next" is executed.

Any thoughts on how this could be implemented in lua would be appreciated,

Martin