OK, I did a quick setup of an environment where [new] does not accept -dash initialised data, but with a separate [new-wish] that has a final argument, a script evaluated inside the object, which can do any pre-initialisation before the constructor is called. This solution works for me now and I'm not having issues with the couple of other XOTcl libraries I'm using.
I think this is a pretty decent solution and the resulting instantiation code actually looks pretty nice and readable. I'm up to other names for "new-with". Maybe something like <new ?args? with ?initscript?.There's a mini-example at the bottom.
Enjoy.
Class SafeInit
SafeInit instproc new {args} { return [next [concat [list -init] $args]] }
SafeInit instproc new-with {args} { set script [lindex $args end] set args [lrange $args 0 end-1] set name [my autoname ::xotcl::stk__#] return [my create $name [list -eval $script] [concat [list -init] $args]] }
# Mix safer [new] and [new-with] into every class Class instmixin SafeInit
# Test:
Class Foo -parameter { {animal horse} }
Foo instproc init {arg1 arg2} { puts "Constructor args: $arg1 $arg2" }
Foo instproc hello {} { puts jou }
# We do not override [create] or the auto-create from [unknown]. Ie. # class creation still works and other stuff :-) puts "Should say 'jou':" Foo ob 1 2 -hello puts "Animal: [ob animal]"
# Instantiate with -dash args set ob [Foo new -hellou world] puts "Animal: [$ob animal]"
# Instantiate with some presets puts "Show say 'jou':" set ob [Foo new-with 1 2 { my hello my animal cat }] puts "Animal: [$ob animal]"