SimpleVariable v1.0
The Simple Development Library typed variables handling
Project: the Simple Development Library.
::Simple::Variable::cget
?-monitortype?::Simple::Variable::configure
?-monitortype extbool?::Simple::Variable::declare
variable ?-description string? ?-monitortype extbool? ?-type type? ?-choices any-list? ?-value any?::Simple::Variable::delete
variable::Simple::Variable::information access
variable::Simple::Variable::information choices
variable::Simple::Variable::information default
variable::Simple::Variable::information defaultgiven
variable::Simple::Variable::information description
variable::Simple::Variable::information exists
variable::Simple::Variable::information monitortype
variable::Simple::Variable::information type
variable::Simple::Variable::IS-ARRAY
::Simple::Variable::NOT-AN-ARRAY
::Simple::Variable::CANT-DELETE
::Simple::Variable::BAD-TYPE-BOOLFLAG
::Simple::Variable::AT-LEAST-TWO-CHOICES
::Simple::Variable::NO-CHOICES-ALLOWED
::Simple::Variable::NO-TYPE-CHOICE-VAR
::Simple::Variable::NO-DEFAULT-VALUE
::Simple::Variable::NO-TYPE-CHOICE-ARRAY
::Simple::Variable::MONITORING-ALIAS
::Simple::Variable::cget
?-monitortype?::Simple::Variable::configure
?-monitortype extbool?::Simple::Variable::declare
variable ?-description string? ?-monitortype extbool? ?-type type? ?-choices any-list? ?-value any?::Simple::Variable::delete
variable::Simple::Variable::information access
variable::Simple::Variable::information choices
variable::Simple::Variable::information default
variable::Simple::Variable::information defaultgiven
variable::Simple::Variable::information description
variable::Simple::Variable::information exists
variable::Simple::Variable::information monitortype
variable::Simple::Variable::information type
variableSynopsis: the Simple Development Library typed variables handling.
Keywords: variable and type.
This package allows to handle typed variables. Typed variables not only have a name and a value if initialized, as regular Tcl variables do, but also a type and a description. Typed variables can be used as non-local (namespace or global) Tcl variables. Typed variables can be monitored to ensure their values conform to their type.
Procedures are provided to declare, delete and get information about typed variables.
Typed variables are declared via ::Simple::Variable::declare
or embedded within programs (declare-program
), packages (declare-package
) or classes (declare-class
). Upon declaration, typed variables can be optionally initialized to a value. The declaration can also specify whether a variable shall be monitored to ensure its values conform to its type. Typed variables can be deleted via ::Simple::Variable::delete
. The ::Simple::Variable::information
procedure provides several subcommands to query information about typed variables: exists
, defaultgiven
, default
, type
, choices
, description
, access
and monitortype
.
# Install the package package require SimplePackage ::Simple::Package::require-and-install SimpleVariable # Configure The Simple Development Library to store the metadata ::Simple::configure -storemetadata true # Declare a typed variable and monitor its value ::Simple::Variable::declare foobar\ -description {may contain foo or bar only}\ -monitortype true -type choice -choices {foo bar} # Get some information about the just declared variable # This displays the following: # |Type: "choice" # |Description: "may contain foo or bar only" puts "Type: \"[::Simple::Variable::information type foobar]\"" puts "Description: \"[::Simple::Variable::information description foobar]\"" # Set the variable to a valid value # This displays the following: # |bar set foobar bar puts $foobar # Set the variable to an invalid value # This throws an error: # can't set "foobar": invalid value "gee" for variable # "::foobar" of type "choice": must be foo or bar set foobar gee
Date | Reason |
23-apr-2000 | First public release, version 0.2 |
10-sep-2001 | Second public release, version 0.4 |
17-feb-2003 | Third public release, version 0.5 |
30-mar-2003 | Extra package, version 0.5.1 |
09-may-2004 | Package renamed to SimpleVariable , version 0.5.2 |
22-jun-2005 | The Simple Development Library version 1.0 |
Copyright (C) 1999-2005, Juan C. Gil (jgil@gmv.es).
Paradigm: procedural.
Requisites: SimpleType 1.0
.
None.
Type: boolean.
Value: 0.
Description: whether to monitor the typed variables to ensure their values conform to their types. Notice that only those variables declared via the ::Simple::Variable::declare
procedure after modifying this option are affected by the change.
::Simple::Variable
::Simple::Variable::Priv
::Simple::Variable::cget
?-monitortype?Synopsis: gets the package options.
Details: see section 4.1.
::Simple::Variable::configure
?-monitortype extbool?Synopsis: configures the package options.
Details: see section 4.2.
::Simple::Variable::declare
variable ?-description string? ?-monitortype extbool? ?-type type? ?-choices any-list? ?-value any?Synopsis: declares a typed variable.
Details: see section 4.3.
::Simple::Variable::delete
variableSynopsis: deletes a typed variable.
Details: see section 4.4.
::Simple::Variable::information access
variableSynopsis: returns a typed variable access mode.
Details: see section 4.5.
::Simple::Variable::information choices
variableSynopsis: returns a typed variable choices.
Details: see section 4.6.
::Simple::Variable::information default
variableSynopsis: returns a typed variable default value.
Details: see section 4.7.
::Simple::Variable::information defaultgiven
variableSynopsis: returns whether a typed variable has a default value.
Details: see section 4.8.
::Simple::Variable::information description
variableSynopsis: returns a typed variable description.
Details: see section 4.9.
::Simple::Variable::information exists
variableSynopsis: returns whether a typed variable exists.
Details: see section 4.10.
::Simple::Variable::information monitortype
variableSynopsis: returns whether a typed variable is being monitored.
Details: see section 4.11.
::Simple::Variable::information type
variableSynopsis: returns a typed variable type.
Details: see section 4.12.
::Simple::Variable::IS-ARRAY
Message: variable "name" is array.
Explanation: non-array variable name could not be declared because it already exists and is an array.
Corrective action: delete the existing array.
::Simple::Variable::NOT-AN-ARRAY
Message: variable "name" isn't array.
Explanation: array name could not be declared because it already exists and is not an array.
Corrective action: delete the existing non-array variable.
::Simple::Variable::CANT-DELETE
Message: can't delete "name": no such variable.
Explanation: variable name could not be deleted because it does not exist.
::Simple::Variable::BAD-TYPE-BOOLFLAG
Message: invalid type "boolflag" for non-flag "name".
Explanation: non-flag name type is "boolflag", but this type is only valid for flags.
Corrective action: either convert the variable or argument to a flag or modify its type.
::Simple::Variable::AT-LEAST-TWO-CHOICES
Message: variable, array element or argument "name" requires at least two choices.
Explanation: variable, array element or argument name is of type "choice" or derived from it, but less than two choices were supplied.
Corrective action: supply at least two choices.
::Simple::Variable::NO-CHOICES-ALLOWED
Message: no choices allowed for variable, array element or argument "name" of type "type".
Explanation: variable, array element or argument name is of type type, which is not "choice" nor derived from it, and so it does not allow the specification of choices.
::Simple::Variable::NO-TYPE-CHOICE-VAR
Message: variable "name" is not of type "choice" nor "choice-list".
Explanation: the typed variable name type is not "choice" nor "choice-list".
::Simple::Variable::NO-DEFAULT-VALUE
Message: variable, [incr Tk] option, argument or option "name" has no default value.
Explanation: the typed variable, [incr Tk] option, argument or option name has no default value as it was not given in its declaration.
::Simple::Variable::NO-TYPE-CHOICE-ARRAY
Message: array "name" is not of type "choice-array" nor "choice-list-array".
Explanation: the typed array name type is not "choice-array" nor "choice-list-array".
::Simple::Variable::MONITORING-ALIAS
Message: type monitoring for aliased variable.
Explanation: the procedure which monitors the typed variables to ensure their values conform to their types was called for a posibly correct typed variable but aliased through the use of the upvar
or global
commands. Due to a Tcl limitation it is then impossible to access the name of the original typed variable, and so its type, so the monitorng procedure can not proceed.
Corrective action: either deactivate the variable type monitoring or use no alias.
Each typed variable internal representation is stored in one element of an array named %Variables% in the variable namespace. The name of the array element is the unqualified variable name. The array element is a list with the following elements:
0 | variable type |
1 | variable choices (empty if type is not "choice" or derived from it) |
2 | variable description |
3 | variable access mode |
4 | whether the variable has a default (initial) value |
5 | variable default (initial) value |
It is possible to declare typed arrays using derived array types as in ::Simple::Variable::declare
foo -type number-array. It is even possible to have an specific type for array elements by declaring them as in ::Simple::Variable::declare
foo(bar) -type float. If both things are done together, all the elements in the array foo must be numbers, but the values of foo(bar) must be float numbers.
It might be useful to be able to handle variables with more than one type so that its values must conform to one of those.
::Simple::Variable::cget
?-monitortype?Synopsis: gets the package options.
Access mode: public.
Arguments:
Argument | Type | Default value/ choices | Description |
-monitortype | boolflag | (n/a) | Gets whether to monitor the typed variables to ensure their values conform to their types |
Returns: the requested option value or the whole list of options if none specified.
::Simple::Variable::configure
?-monitortype extbool?Synopsis: configures the package options.
Access mode: public.
Arguments:
Argument | Type | Default value/ choices | Description |
?-monitortype? | extbool | false | Whether to monitor the typed variables to ensure their values conform to their types |
Returns: the empty string.
::Simple::Variable::declare
variable ?-description string? ?-monitortype extbool? ?-type type? ?-choices any-list? ?-value any?Synopsis: declares a typed variable.
Access mode: public.
This procedure declares a non-local (namespace or global) typed variable or array and optionally assign a value to it.
If -type is given, the variable type is set to its parameter, otherwise the variable type is set to any.
When the variable type is choice or derived from it, -choices is required.
Use -value to initialize the typed variable to a value. This actually creates the variable.
The -monitortype flag allows to override the package option of the same name for the varible being declared. This option controls whether the variable will be monitored to ensure its values conform to its type. An error is thrown if a variable being monitored is set to a value which does not conform to its type, but the variable still contains the non-conforming value.
Arguments:
Argument | Type | Default value/ choices | Description |
variable | variablename | (n/a) | Variable name |
?-description? | string | (empty string) | Variable description |
?-monitortype? | extbool | false | Whether to enable run-time type monitoring |
?-type? | type | any | Variable type |
?-choices? | any-list | (empty string) | List of choices for choice type and its derivations |
?-value? | any | (n/a) | Variable value |
Returns: the empty string.
The type monitoring is achieved by setting a write trace on the variable via the trace
procedure with the ::Simple::Variable::Priv::trace-monitor
as handler procedure. This write trace is only set for monitored variables.
The order of the flags is irrelevant but their parameters can not start by an hyphen.
::Simple::Variable::delete
variableSynopsis: deletes a typed variable.
Access mode: public.
This procedure deletes a typed variable declared via the ::Simple::Variable::declare
procedure.
Arguments:
Argument | Type | Default value/ choices | Description |
variable | variablename | (n/a) | Variable name |
Returns: the empty string.
::Simple::Variable::information access
variableSynopsis: returns a typed variable access mode.
Access mode: public.
Arguments:
Argument | Type | Default value/ choices | Description |
variable | variablename | (n/a) | Variable name |
Returns: the typed variable access mode.
::Simple::Variable::information choices
variableSynopsis: returns a typed variable choices.
Access mode: public.
Arguments:
Argument | Type | Default value/ choices | Description |
variable | variablename | (n/a) | Variable name |
Returns: the typed variable choices.
::Simple::Variable::information default
variableSynopsis: returns a typed variable default value.
Access mode: public.
Arguments:
Argument | Type | Default value/ choices | Description |
variable | variablename | (n/a) | Variable name |
Returns: the typed variable default value.
The ::Simple::Option::information default
procedure.
The ::Simple::ItkOption::information default
procedure.
::Simple::Variable::information defaultgiven
variableSynopsis: returns whether a typed variable has a default value.
Access mode: public.
Arguments:
Argument | Type | Default value/ choices | Description |
variable | variablename | (n/a) | Variable name |
Returns: whether the typed variable has a default value.
The ::Simple::Option::information defaultgiven
procedure.
The ::Simple::ItkOption::information defaultgiven
procedure.
::Simple::Variable::information description
variableSynopsis: returns a typed variable description.
Access mode: public.
Arguments:
Argument | Type | Default value/ choices | Description |
variable | variablename | (n/a) | Variable name |
Returns: the typed variable description.
::Simple::Variable::information exists
variableSynopsis: returns whether a typed variable exists.
Access mode: public.
Arguments:
Argument | Type | Default value/ choices | Description |
variable | variablename | (n/a) | Variable name |
Returns: whether the typed variable exists.
::Simple::Variable::information monitortype
variableSynopsis: returns whether a typed variable is being monitored.
Access mode: public.
This procedure returns whether a typed variable is being monitored to ensure its values conform to its type.
Arguments:
Argument | Type | Default value/ choices | Description |
variable | variablename | (n/a) | Variable name |
Returns: whether the typed variable is being monitored.
The ::Simple::ItkOption::information monitortype
procedure.
::Simple::Variable::information type
variableSynopsis: returns a typed variable type.
Access mode: public.
Arguments:
Argument | Type | Default value/ choices | Description |
variable | variablename | (n/a) | Variable name |
Returns: the typed variable type.