[Xotcl] init Constructor
Gustaf Neumann (sslmail)
neumann at wu.ac.at
Sun May 26 12:05:35 CEST 2024
A small addon for explanation: Given the following definition
nx::Class create C {
:property {foo 1}
:public method m1 {{-np1 1} {-np2 2} p1 p2} {
# ...
}
:method init {} {
puts stderr "[self] init, instance vars <[:info vars]>"
}
}
C create c1 -foo 2
C create c2 -foo 3 {
puts stderr "[self] initblock, instance vars <[:info vars]>"
set :x 1
}
puts stderr SYNTAX=[C info lookup syntax create]
> /objectName/ ?-foo /value/? ?-object-mixins /mixinreg .../? ?-object-filters /filterreg .../? ?-class /class/? ?/__initblock/?
When the “create” method is called, it processes its arguments exactly the same way as when e.g. the method “m1” is called. The argument list can contain positional and non-positional arguments. By the definition of nx, the positional arguments are collected via the inheritance path, and these are followed by an optional “::initblock” (see last line above). The init-block is used, when “c2” is created, and executed before “init” is called. In the init-block, one can set variables, call arbitrary methods, etc. So, if you need parameter passing to superclasses, call there your “own” method “inititialize” with arguments, (or call it from “init”). The init-block can be provided for classes, objects, and also for properties. So this is fairly regular in nx.
Of course, one can “fiddle” with the setup and sneak in additional positional arguments (as Stefan mentioned), but I would not recommend going this way, since we have currently no “high-level” commands to ease this step.
all the best
-g
More information about the Xotcl
mailing list