Version 1 of Object method ensemble

Updated 2009-07-09 23:50:33 by mpdanielson

I'd like to be able to define a method that was an ensemble. I can hack it by using the namespace ensemble command in the objects namespace and defining procs, but doing so bypasses the normal object mechanism. The syntax I'd like to use is something like:

oo::class create C {
  method {foo boo} { args } { puts "boo called with $args" }
  method {foo goo} { args } { puts "goo called with $args" }
  method {foo doo} { args } { puts "goo called with $args" }
}

which could be invoked as:

(test) 1 % set o C new ::oo::Obj12 (test) 2 % $o foo boo 1 boo called with 1 (test) 3 % $o foo goo 2 goo called with 2 (test) 4 % $o foo doo 3 doo called with 3 (test) 5 % $o foo bar 4 unknown or ambiguous subcommand "bar": must be boo, doo, or goo (test) 6 %

What I can do now to get the effect is this:

oo::class create C {
  constructor { args } {
    proc boo { args } { puts "boo called with $args" }
    proc goo { args } { puts "goo called with $args" }
    proc doo { args } { puts "doo called with $args" }
    namespace ensemble create -command foo -subcommands [list boo goo doo]
    ::oo::objdefine [self] forward foo [namespace current]::foo
  }
}

Which results in an very un-object-like object. Am I missing the "right" way to do this?

Thanks, Paul