View · Search · Index
No registered users in community xowiki
in last 10 minutes

[Xotcl] snit like delegation for xotcl

From: Gustaf Neumann <neumann_at_wu-wien.ac.at>
Date: Sun, 29 Feb 2004 23:19:42 +0100

Dear XOTcl community,

since someone asked for it, here comes a simple implementation
of delegation similar as done in snit (unknown calls are delegated).
Maybe, someone can use it.

best regards
-gustaf neumann

##############################################################
# define a deletate method and a default behavior for unknown
##############################################################
Object instproc delegate {method obj} {
  my set delegate($method) $obj
}
Object instproc unknown {m args} {
  if {[my exists delegate($m)]} {
    eval [my set delegate($m)] $m $args
  } elseif {[my exists delegate(*)]} {
    eval [my set delegate(*)] $m $args
  }
}

##############################################################
# some syntactic sugar for defining methods
##############################################################
Class instproc methods list {
  foreach {name arguments body} $list {
    my instproc $name $arguments $body
  }
}

##############################################################
# example from the snit homepage: A dog with a tail....
##############################################################
Class Tail -parameter {{length 5}} -methods {
  wag {} {
    puts "[my info parent] Wag, wag, wag."
  }
}

Class Dog -methods {
  init {} {
    set tail [Tail new -childof [self]]
    my delegate * $tail
  }
  bark {} {
    puts "[self] Bark, bark, bark."
  }
}


Dog fido
fido wag
fido bark

==========================================================
-- 
Univ.Prof. Dr.Gustaf Neumann
Abteilung für Wirtschaftsinformatik
WU-Wien, Augasse 2-6, 1090 Wien