View · Search · Index
No registered users in community xowiki
in last 10 minutes

Re: [Xotcl] nx parameters

From: Gustaf Neumann <neumann_at_wu-wien.ac.at>
Date: Mon, 20 Dec 2010 09:03:58 +0100

On 19.12.10 21:37, Victor Mayevski wrote:
> Although your examples do seem to work on Object objects, the Class
> instances do not work right.
They work "right", but maybe not as you expected.
You original question was:

> Is there any way to add meta-data to parameters during parameter
> creation? Specifically I would like to add a time stamp to the the
> parameter so I can list parameters in the order they were created.

The following code snippet adds meta-data to every
"attribute" (similar
to "-parameters" in XOTcl) when it is created. More
technically, the method
"attribute" creates a slot-object on the class or object, on
which it is created.
These slot objects are extended in this example by an
additional instance
variable named "timestamp".
> nx::Class create C {
> # Create attributes "x" and "y" and script the initialization of
> # these attributes.
> :attribute x {
> set :timestamp [clock clicks]
> }
> :attribute y {
> set :timestamp [clock clicks]
> }
> :create c1
> }
Note, that the slot objects are created at the time, when
the method
"attribute" is called, in the code above at the time, when
class "C" is
created, before the object c1.

Slot objects provide common meta-data for all their managed
instance
variables. Typical examples for such meta-data are a default
value,
a value checker, a linkage to a database table/attribute,
the sql-type of the attribute, a label for pretty printing,
a widget
for data entry, .....). If an attribute is defined on a
class, it is
created per-class, not per class instance (that would be too
costly in most situations).
> if I do [C create c2] and then [print_slots_and_timestamps c2], the
> actual output is still for instance c1, not c2,
of, course, if one creates two instances of C (such as c1
and c2),
these will share the same slot objects (in the example
above, the slot
objects are named C::slot::x and C::slot::y; normally, the names
of the slot objects won't interest you).

If you want to create unique timestamps per attribute value,
i would recommend to combine a timestamp from the object
creation time with the a timestamp from the slot object
creation (see below)

best regards
-gustaf neumann

========================================
proc print_slots_and_timestamps {obj} {
   foreach slot [$obj info lookup slots] {
     if {[$slot eval {info exists :timestamp}]} {
       puts "$obj has attribute [$slot name] \
          timestamp [$obj T].[$slot eval {set :timestamp}]"
     }
   }
}
nx::Class create C {
   :attribute x {set :timestamp [clock clicks]}
   :attribute y {set :timestamp [clock clicks]}
   :attribute {T "[clock clicks]"}
   :create c1
   :create c2
}
print_slots_and_timestamps c1
print_slots_and_timestamps c2