Hello, is it possible to pass data to init constructor in NX? For example smth like that:
nx::Class create hello {
:method init {args} { if {[llength $args] > 0} { foreach name $args { puts "Hello: $name" } } }
}
::hello create helloObj [list Bruno Pedro]
Without using properties, because I want to set up properties from init method. Thank you
Hi Maksym!
Thank you for your Email!
Hello, is it possible to pass data to init constructor in NX?
No, unless you would fiddle around (see below), NX, by default, does not allow doing so (for good reasons).
Without using properties, because I want to set up properties from init method.
What do you want to achieve? You can still process (e.g., validate) the properties and object variables from within init, as it will be called. But it won't receive, by default, the residual arguments to object construction.
You should use an initblock script to call init explicitly, passing in your arguments. (NX will ensure that the init is not called internally for a second time.) Still, I recommend sticking with the property mechanism. HTH, Stefan
==
nx::Class create hello { :method init {args} { if {[llength $args] > 0} { foreach name $args { puts "Hello: $name" } } } }
::hello create helloObj [list :init [list Bruno Peter] ]
Thank you _______________________________________________ Xotcl mailing list Xotcl@alice.wu.ac.at http://alice.wu.ac.at/mailman/listinfo/xotcl
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