Language Reference
This section describes the details of the predefined objects in Frag.
Many of them are implemented as Command Objects (see Section
), which is - as a convention for the
predefined Frag objects -
indicated by an object name
starting with a lower-case character. Others are predefined classes, such as
Object (see Section ); those objects have object
names starting with an upper-case character - also as a convention for the
predefined Frag objects.
eval ?-uplevel ?levels?|-global? <code>
eval is used to dynamically evaluate the code given by the <code> argument. It
can optionally have the eval -uplevel ?levels? argument, which allows code to be evaluated
levels level up the callstack. The default value for the option levels argument is 1,
which means that the code will be evaluated in the calling callframe.
Another option is to use eval -global. This option evaluates the code in the global
callframe. Please note that in general, it is recommended to avoid the use of global
variables, which could be accessed via this option.
The following code produces some code on the fly (it creates a number of class objects X_1 X_2 X_3), and
then the produced code is executed via eval.
set code ""
foreach cl {X_1 X_2 X_3} {
set code [string append $code " Object create $cl\n"]
}
puts "Code produced: \n$code"
eval $code
The following command uses the uplevel option to read a variable from the
calling callframe:
Command create c1 -cmd {} {
eval -uplevel {get ix}
}
set ix 1
puts "ix = [c1]"
Because in this example the variable ix is defined at the global callframe,
eval -global {get ix} would produce the same result.
The Hashtable class basically is an additional convenience interface for
treating the variable table of an object like a Hashtable (as for instance
in Java). A Hashtable can be created using Hashtable create ... or it can
be added to an object using classes. Both, the ordinary methods
of Object, such as get and set can be applied on a hashtable, as well
as the hashtable-specific methods explained below. All these methods operate on
the same data: the object's variable table (which is internally implemented
as a hashtable).
The following example creates a hashtable object h1 and initializes it
with setList. Then one variable c is unset and another variable tt is
set, using the ordinary set and unset methods from object. Finally,
the contents of the modified Hashtable are printed using getList.
set h1 [Hashtable create -setList {a b c d r {1 2 3 4} v {7 8}}]
$h1 unset c
$h1 set tt xx
puts "hashtable content: [$h1 getList]"
You can add the hashtable to each existing object or class (as a class, superclass
or mixin),
to benefit from its additional methods:
Object create MyClass
set mc [MyClass create]
$mc classes [$mc getClasses] Hashtable
# now hashtable method can be used
$mc setList {a 1 b 2}
puts "hashtable content: [$mc getList]"
# remove hashtable again
$mc classes MyClass
# the invocation "$mc setList {a 1 b 2}" would raise an error now
<hashtableInstance> clear
clear removes all elements from the hashtable.
The following prints Keys = because h1 is cleared completely.
set h1 [Hashtable create h1 -setList {a b c d}]
$h1 clear
puts "Keys = [$h1 keys]"
<hashtableInstance> containsKey <key>
containsKey returns true, if a hashtable contains the given key
argument, or false if not.
The following code checks for two keys in a hashtable and prints:
Contains a? true, contains x? false.
set h [Hashtable create -setList {a b c d}]
puts "Contains a? [$h containsKey a], contains x? [$h containsKey x]"
<hashtableInstance> getList
getList returns the contents of the complete hashtable as a key/value
pair list.
The following code initializes a hashtable, does a few modifications to the
data, and then uses getList to print the contents. The result is:
Contents: x 2 v {7 8} r {1 2 3 4} c dset h [Hashtable create -setList {a b c d r {1 2 3 4} v {7 8}}]
$h set x 2
$h unset a
puts "Contents: [$h getList]"
<hashtableInstance> keys
keys returns a list of all "keys" in the variable hashtable.
The following lists the keys in the hashtable and prints Keys = a c.
set h1 [Hashtable create h1 -setList {a b c d}]
puts "Keys = [$h1 keys]"
<hashtableInstance> setList <key value pair list>
setList fills the variable hashtable with the data from the key/value
pair list given as an argument. The key/value pair list must have an even
number of list elements.
The following code initializes the variable hashtable via a list and prints
the results. setList overwrites existing content, but does not delete the
contents of the hashtable before writing. Hence, the result of the following
code is: Contents: x d a c v {7 8} r {1 2 3 4} c dset h [Hashtable create -setList {a b c d r {1 2 3 4} v {7 8}}]
$h setList {a c x d}
puts "Contents: [$h getList]"
<hashtableInstance> size
size returns the number of the elements in the hashtable as an integer.
The following code initializes the hashtable with 4 key/value pairs, then
adds another element, and then unsets an element. So the result is a size
of 4 elements: Size: 4.
set h [Hashtable create -setList {a b c d r {1 2 3 4} v {7 8}}]
$h set x 2
$h unset a
puts "Size: [$h size]"
<hashtableInstance> values
values prints all values in the hashtable.
The result of the following code is: Values: 2 {7 8} {1 2 3 4} dset h [Hashtable create -setList {a b c d r {1 2 3 4} v {7 8}}]
$h set x 2
$h unset a
puts "Values: [$h values]"
Hosted by |