aitcs - Array in the Command's Shadow
An Array in the Command's Shadow is an array with an associated set of commands that each provide access to or allow certain useful manipulations of the array.
An AitCS may be used where a program would normally use global or namespace variables to hold configuration, state, widget paths, temp values, etc.
Arrays are named+randomly-named and created in a namespace. Several commands (via aliases) are created in a (possibly different) namespace to manipulate the array.
The name ends up being used much like the set command.
The inspiration came from being tired of having to use the variable command in almost all the procs in a namespace when often in a given proc there is interest only in a few items in the global state.
By reducing array manipulation clutter this extension may help in improving program clarity!
Names a new array in the namespace variable_namespace. The format of the name is "namespace::__name__cccccc_" where "cccccc" is six random characters in the range a-z. If variable_namespace isn't supplied or is an empty string then the array will be named in the caller's namespace. Doesn't create an array variable. Returns the name of the newly named array.
Creates a new array in the namespace variable_namespace and creates commands (via aliases) in the command_namespace to manipulate the array. The format of the name and the variable_namespace will be as described in the name ensemble subcommand. If the name isn't supplied or is an empty string then the name will be set to a string of six random characters in the range a-z. If the command_namespace isn't supplied or is an empty string then the command_namespace will be set to the variable_namespace. The commands (via aliases) are created in the command_namespace. Initial values are set from key val... if supplied. See the AITCS COMMANDS section. Creates an array variable in the variable_namespace. Returns the name of the newly named array, or the randomly-generated name if the name argument wasn't supplied.
Commands (via aliases) are created in the command_namespace and are based on the name supplied when the array was created.
Causes the array to come into existence in the current context. This is upvar #0 varname name. From then on in the current context the array can be referenced by name. Returns an empty string. Nickname: Make Exist.
Gets the value of one element of the array or sets the value of one or more elements of the array. The value(s) for the key(s) will be set using array set. Returns the value at key. Nickname: Get/Set.
Returns true if the key exists in the array or false if the key doesn't exist in the array. If the key exists and myVar is supplied and is not an empty string then the variable it names will be set in the caller's context to the value at key. Nickname: Exists.
Removes key(s) from the array. The array unset command is used, allowing key(s) to contain wildcards. It isn't an error if a key doesn't exist in the array. Returns an empty string. Nickname: Unset.
Returns the value at key if it exists, otherwise returns default, or an empty string if default isn't supplied. If myVar is supplied and is not an empty string then the variable it names will be set in the caller's context to the returned value. Nickname: Get With Default.
Returns the fully namespace qualified name of the array if no key supplied or if key is an empty string, otherwise returns the fully namespace qualified name of the array and the element key. Nickname: Variable Reference.
Returns the fully namespace qualified command. Nickname: Command Reference.
Every key is set to val. Returns val. Nickname: Setto.
Every val is lappended to the element at key. Returns the value at key. Nickname: Lappend.
Increments the value at key by incr (default 1). Returns the value at key. Nickname: Incr.
The key should be another AitCS. Permits "nesting". Calls are chained. Nickname: Struct Dereference.
Unsets the array and deletes all the commands. Returns an empty string. Nickname: Destroy.
# Set/Get/Variable Reference # Save a widget and link a textvariable aitcs new rec aitcs new gui set fld preference rec $fld Tcl gui $fld [entry .e -textvariable [rec& $fld]] [gui $fld] insert end /Tk string length [rec preference]; # 6 # Get/Set aitcs new things things a 3 things b [expr {[things a] + 2}] things c [expr {[things b] * [things a]}] things c; # 15 # Exists aitcs new colours colours red #FF0000 colours? red; # 1 colours? green; # 0 colours? red color1; # 1 info exists color1; # 1 set color1; # #FF0000 colours? green color2; # 0 info exists color2; # 0 # Setto/Exists/Unset/Variable Reference aitcs new fruits fruits= apple orange pear apricot 0 if {![fruits? peach]} { fruits= peach 0 } array names [fruits&]; # {apple orange pear apricot peach} fruits- p* a* array names [fruits&]; # {orange} # Get With Default aitcs new things things are good things^ are ok; # good things^ be ok; # ok things^ willbe; # Empty string things^ is nice here; # nice, and var here is set to nice # Lappend aitcs new store store this one store+ this two three four five store this; # {one two three four five} # Incr aitcs new counters counters cycles 0 counters+= cycles counters+= cycles 2 counters+= cycles -1 counters cycles; # 2 # Make Exist/Exists aitcs new cow cow! set cow(sound) moo if {[cow? sound] && $cow(sound) eq "moo"} { puts "Standard English Cow" } # Command Reference/Variable Reference/Incr aitcs new callback callback count 0 after 1000 [list [callback&&]+= count] vwait [callback& count] callback count; # 1 # Struct Dereference/Random Name/Initial Values aitcs new animals {} {} dog [aitcs new {} {} {} says woof likes cat] cat [aitcs new {} {} {} says meow likes dog] animals home [list dog cat] set res ""; foreach animal [animals home] { append res $animal " says " [animals. $animal says] append res " likes " [animals. $animal likes] append res " who says " [animals. [animals. $animal likes] says]\n } set res # dog says woof likes cat who says meow # cat says meow likes dog who says woof # Struct Dereference aitcs new outer aitcs new inner aitcs new innest inner count 6 innest things [list] innest+ things 1 2 outer member inner inner item innest outer. member+= count 2; # 8 outer. member. item+ things 3 4; # {1 2 3 4} outer. member. item^ name; # Empty string # Destroy aitcs new tmp tmp count 5 tmp+= count -1 tmp count; # 4 set fqarr [tmp&] info exists $fqarr; # 1 tmp--- info exists $fqarr; # 0 tmp count; # <error>
alias, array, command, eagle, namespace, snake
Programming tools
Copyright © 2020 Stuart Cassoff <stwo@users.sourceforge.net>