Hello everyone,
Is there any way to pass arguments to the init method in NX (as in XOTcl)? Or is using object parameters on initialization the preferred way in NX to initialize an objects instance variables?
Thanks, Arthur
Dear Arthur,
The short answer is: object parameter are the preferred way, these fit better to dynamic languages and avoid several potential anomalies (see e.g. the example from the slides of the 2011 Tcl conference)
The longer answer (not necessarily introductory): For NX the object parameters are the only way, but the object parameters can be positional and/or non-positional, as for parameter definitions for methods. The object signature is derived from the slots of the class hierarchy from which an object is instantiated. The derived signature is a sequence of positional and non-positional parameter specs. For object parameters, typically most parameter specs are non-positional, just the last spec is positional (the init script). Certainly, this can be extended:
Here is a short example.
nx::Class create C { :property x } nx::Class create D -superclass C { :property y } D create d1 -y 123
This script implies an object signature for D of the form "-x -y .... {init-script}" (simplified). If one adds a positional parameter
nx::Class create E -superclass D { :property {z ""} {set :positional 1} }
the signature of E becomes "z -x -y ... {init-script}" (also simplified). With this signature, one can create objects e.g. via
E create e1 "hello world"
For the positional parameters, a position can be added (the non-pos paramters start at position 1, so use e.g. 0.1, 0.2 etc.). However, for beginners i would recommend to stick to the default behavior with just non-positional object parameters...
best regards Gustaf Neumann
PS: XOTcl (and XOTcl 2) support method parameters for constructors, and since XOTcl 2 and NX are based on nsf, this could be done as well in NX (somewhat contradictory to above, but such a modification would require low-level modifications to NX, it would not be the same NX). However, when method parameters to constructors should be as well positional and non-positional, these tend to become a source for confusion, at least, if one wants to support derived object-parameters....
On 28.11.11 21:53, Arthur Schreiber wrote:
Hello everyone,
Is there any way to pass arguments to the init method in NX (as in XOTcl)? Or is using object parameters on initialization the preferred way in NX to initialize an objects instance variables?
Thanks, Arthur