Re: Referencing values in an array
- From: stefan <stefan.sobernig@xxxxxxxxxxxxx>
- Date: Mon, 28 Jul 2008 11:20:04 -0700 (PDT)
Daniel,
Using the following structure instead leads to the wanted results:
MyShape shape$i $i;
set shapes($i) shape$i;
Just some remarks from my side ...
1. In the original line ...
MyShape shape $i;
... you stumbled over the explicit naming of the MyShape instance.
"shape",
as created above, will be re-created (in XOTcl terminology) in each
iteration
of the loop. By setting implicitly naming the objects ("shape$i") you
definitely solve the issue,
but XOTcl offers you alternatives. I personally find that encoding the
value $i three times
(object name, attribute id, key of shapes array) is a litte
superfluous for my taste.
2. You seem to devise the shapes array as devise to store MyShape
instances in
an addressable "store". Well, you double your efforts here, that's
what an object system
should do for you. XOTcl does (in many flavours).
Consider the following:
package req XOTcl
namespace import ::xotcl::*
Class MyShape -slots {
Attribute id
}
#
# In XOTcl, class are class-objects, that is
# they may be declared per-object behaviour
# (think of 'static' in class-centric languages
# such as Java, C#, ...)
#
MyShape proc getShapes {} {
for {set i 0} {$i < 5} {incr i} {
#
# 1. [self] unfolds to ::MyShape,
# i.e. is the self-reference
# to the current object ...
#
# 2. "new" assigns an auto-generated
# identifier to the created object.
set shape [[self] new -id $i]
puts "Orig: [$shape id]";
}
#
# Well, the class object can inform
# you about existing offsprings ...
# You may access this information
# by means of XOTcl's introspection:
# ::xotcl::Object->info() or
# ::xotcl::Class->info()
foreach s [::MyShape info instances] {
puts "After: [$s id]";
}
}
MyShape getShapes
.
- Follow-Ups:
- Re: Referencing values in an array
- From: Daniel Kirsch
- Re: Referencing values in an array
- References:
- [XOTcl] Referencing values in an array
- From: Daniel Kirsch
- Re: [XOTcl] Referencing values in an array
- From: Daniel Kirsch
- [XOTcl] Referencing values in an array
- Prev by Date: Re: Tcl_SetStringObj question
- Next by Date: Re: New Canvas
- Previous by thread: Re: [XOTcl] Referencing values in an array
- Next by thread: Re: Referencing values in an array
- Index(es):
Relevant Pages
|