SimpleType v1.0 

 The Simple Development Library types handling 

Project: the Simple Development Library.

Table of contents

1. Package description
   1.1 Overview
   1.2 Usage
   1.3 Examples
   1.4 History
   1.5 Copyright
2. Package public API
   2.1 Types
      2.1.1 integer
      2.1.2 float
      2.1.3 decimal
      2.1.4 hexadecimal
      2.1.5 binary
      2.1.6 octal
      2.1.7 anybase
      2.1.8 number
      2.1.9 character
      2.1.10 string
      2.1.11 alphabetic
      2.1.12 alphanumeric
      2.1.13 boolean
      2.1.14 extbool
      2.1.15 word
      2.1.16 script
      2.1.17 expression
      2.1.18 list
      2.1.19 channel
      2.1.20 namespace
      2.1.21 qualifiedname
      2.1.22 unqualifiedname
      2.1.23 name
      2.1.24 commandname
      2.1.25 unqualifiedcommandname
      2.1.26 qualifiedvariablename
      2.1.27 unqualifiedvariablename
      2.1.28 variablename
      2.1.29 variableorcommandname
      2.1.30 pattern
      2.1.31 regexp
      2.1.32 formatstring
      2.1.33 widget
      2.1.34 type
      2.1.35 flag
      2.1.36 optional
      2.1.37 declaration
      2.1.38 date
      2.1.39 choice
      2.1.40 any
      2.1.41 boolflag
      2.1.42 url
   2.2 Options
   2.3 Namespaces
   2.4 Commands
      2.4.1 ::Simple::Type::assert-is type value ?extra? ?description?
      2.4.2 ::Simple::Type::cget
      2.4.3 ::Simple::Type::configure
      2.4.4 ::Simple::Type::convert binary integer
      2.4.5 ::Simple::Type::convert boolean extBoolean
      2.4.6 ::Simple::Type::convert decimal integer
      2.4.7 ::Simple::Type::convert flag flag
      2.4.8 ::Simple::Type::convert hexadecimal integer
      2.4.9 ::Simple::Type::convert octal integer
      2.4.10 ::Simple::Type::convert optional optional
      2.4.11 ::Simple::Type::declare type ?-description string? ?-matchingscript script?
      2.4.12 ::Simple::Type::delete type
      2.4.13 ::Simple::Type::information declared ?typePattern?
      2.4.14 ::Simple::Type::information description type
      2.4.15 ::Simple::Type::information exists type
      2.4.16 ::Simple::Type::information matchingscript type
      2.4.17 ::Simple::Type::is type value ?extra?
      2.4.18 ::Simple::Type::modify description type description
      2.4.19 ::Simple::Type::modify matchingscript type matchingScript
      2.4.20 ::Simple::Type::outside-range range value
   2.5 Errors
      2.5.1 ::Simple::Type::BAD-DERIVED-TYPE
      2.5.2 ::Simple::Type::DERIVED-TYPE-OUT-OF-CONTEXT
      2.5.3 ::Simple::Type::NEGATIVE-INT
      2.5.4 ::Simple::Type::ALREADY-EXISTS
3. Package further information
   3.1 Details
   3.2 Todo
4. Package command details
   4.1 ::Simple::Type::assert-is type value ?extra? ?description?
      4.1.1 Command description
         4.1.1.1 Overview
      4.1.2 Command public API
      4.1.3 Command further information
         4.1.3.1 Remarks
   4.2 ::Simple::Type::cget
      4.2.1 Command description
      4.2.2 Command public API
   4.3 ::Simple::Type::configure
      4.3.1 Command description
      4.3.2 Command public API
   4.4 ::Simple::Type::convert binary integer
      4.4.1 Command description
         4.4.1.1 Overview
      4.4.2 Command public API
   4.5 ::Simple::Type::convert boolean extBoolean
      4.5.1 Command description
         4.5.1.1 Overview
         4.5.1.2 Examples
      4.5.2 Command public API
   4.6 ::Simple::Type::convert decimal integer
      4.6.1 Command description
         4.6.1.1 Overview
      4.6.2 Command public API
   4.7 ::Simple::Type::convert flag flag
      4.7.1 Command description
      4.7.2 Command public API
   4.8 ::Simple::Type::convert hexadecimal integer
      4.8.1 Command description
         4.8.1.1 Overview
      4.8.2 Command public API
   4.9 ::Simple::Type::convert octal integer
      4.9.1 Command description
         4.9.1.1 Overview
      4.9.2 Command public API
   4.10 ::Simple::Type::convert optional optional
      4.10.1 Command description
      4.10.2 Command public API
   4.11 ::Simple::Type::declare type ?-description string? ?-matchingscript script?
      4.11.1 Command description
         4.11.1.1 Overview
         4.11.1.2 Examples
      4.11.2 Command public API
      4.11.3 Command further information
         4.11.3.1 Remarks
   4.12 ::Simple::Type::delete type
      4.12.1 Command description
      4.12.2 Command public API
   4.13 ::Simple::Type::information declared ?typePattern?
      4.13.1 Command description
         4.13.1.1 Overview
      4.13.2 Command public API
      4.13.3 Command further information
         4.13.3.1 Remarks
   4.14 ::Simple::Type::information description type
      4.14.1 Command description
      4.14.2 Command public API
   4.15 ::Simple::Type::information exists type
      4.15.1 Command description
         4.15.1.1 Overview
      4.15.2 Command public API
   4.16 ::Simple::Type::information matchingscript type
      4.16.1 Command description
      4.16.2 Command public API
   4.17 ::Simple::Type::is type value ?extra?
      4.17.1 Command description
         4.17.1.1 Overview
      4.17.2 Command public API
      4.17.3 Command further information
         4.17.3.1 Remarks
   4.18 ::Simple::Type::modify description type description
      4.18.1 Command description
      4.18.2 Command public API
   4.19 ::Simple::Type::modify matchingscript type matchingScript
      4.19.1 Command description
      4.19.2 Command public API
   4.20 ::Simple::Type::outside-range range value
      4.20.1 Command description
      4.20.2 Command public API

1. Package description

Synopsis: the Simple Development Library types handling.

Keywords: type, decimal, hexadecimal, binary, octal, base, convert, range and array.

1.1 Overview

This package allows to handle types. A type associates a type name to a matching script used to assess whether a value conforms to the type.

Procedures are provided to declare (although a wealth of predefined types are supplied) delete and modify types, get information about a type and obtain the list of types, as well as several others to convert between different types. ::Simple::Type::is allows to assess whether a value conforms to a type.

1.2 Usage

Types are declared via ::Simple::Type::declare or embedded within programs (declare-program) or packages (declare-package). Types can be deleted via ::Simple::Type::delete or modified via ::Simple::Type::modify. The ::Simple::Type::information procedure provides several subcommands to query information about types: exists, declared, description and matchingscript.

The ::Simple::Type::convert set of procedures convert a value between different types. The ::Simple::Type::is procedure returns whether a value conforms to a type.

There are two kind of types: basic and derived. Derived types are derived from basic types and are denoted as basic type-derivation where basic type is the basic type name (such as integer) and derivation is the kind of derivation. All basic types can be derived but boolflag. Four derivations are currently supported: arrays, pairs, lists and ranges:

The double derivation numeric-range-list is also valid and corresponds to a list of numeric ranges, such as {1 -2:3 :100} for the integer-range-list type. As the double derivation type-list-array is always valid for any type but boolflag, it follows that the triple derivation numeric-range-list-array is also valid.

Derived types are only accepted by the ::Simple::Type::is and ::Simple::Type::information exists procedures.

1.3 Examples

1.4 History

Date Reason
19-feb-1999 Unreleased first version 0.1
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
29-mar-2003 Extra package, version 0.5.1
09-jan-2004 Derived type range accepts equal limits, version 0.5.2
18-nov-2004 Added ::Simple::Type::assert-is, version 0.5.3
22-jun-2005 The Simple Development Library version 1.0

1.5 Copyright

Copyright (C) 1999-2005, Juan C. Gil (jgil@gmv.es).

2. Package public API

Paradigm: procedural.

Requisites: SimpleSubcommand 1.0.

2.1 Types

2.1.1 integer

Description: integer number: +1, 0, -192, ...

2.1.2 float

Description: floating point number: -1.2e23, +3.0, ...

2.1.3 decimal

Description: decimal number: 1234, ...

2.1.4 hexadecimal

Description: hexadecimal number: xF12, ...

2.1.5 binary

Description: binary number: b01011, ...

2.1.6 octal

Description: octal number: 0723, ...

2.1.7 anybase

Description: decimal, hexadecimal, binary or octal number.

2.1.8 number

Description: any number.

2.1.9 character

Description: character.

2.1.10 string

Description: string.

2.1.11 alphabetic

Description: alphabetic (letters only).

2.1.12 alphanumeric

Description: alphanumeric (letters and digits).

2.1.13 boolean

Description: boolean: 0 or 1.

2.1.14 extbool

Description: Tcl extended boolean: 0, 1, true, false, ...

2.1.15 word

Description: Tcl word.

2.1.16 script

Description: Tcl script.

2.1.17 expression

Description: Tcl expression: 2 + 3, ...

2.1.18 list

Description: Tcl list.

2.1.19 channel

Description: Tcl channel: stdin, file0, sock2, ...

2.1.20 namespace

Description: Tcl namespace: ::foo::bar, ...

2.1.21 qualifiedname

Description: Tcl qualified name: ::foo::Bar, ::foo::bar, ...

2.1.22 unqualifiedname

Description: Tcl unqualified name: foo, Bar, ...

2.1.23 name

Description: Tcl qualified or unqualified name: foo, ::Bar, ...

2.1.24 commandname

Description: Tcl command name with optional subcommand: foo bar, ::Gee zuu, ...

2.1.25 unqualifiedcommandname

Description: Tcl command name with optional subcommand: foo bar, Gee zuu, ...

2.1.26 qualifiedvariablename

Description: Tcl qualified variable name: ::foo::Bar, ::foo::bar(gee), ...

2.1.27 unqualifiedvariablename

Description: Tcl unqualified variable name: foo, Bar(gee), ...

2.1.28 variablename

Description: Tcl qualified or unqualified variable name: foo, ::Bar(gee), ...

2.1.29 variableorcommandname

Description: Tcl qualified or unqualified variable or command name: ::Bar(gee), bar gee, ...

2.1.30 pattern

Description: Tcl pattern: foo*bar, ...

2.1.31 regexp

Description: Tcl regular expression: [A-Z]+, ...

2.1.32 formatstring

Description: Tcl format string.

2.1.33 widget

Description: Tk widget: .foo.bar, ...

2.1.34 type

Description: type name: integer, ...

2.1.35 flag

Description: flag: -name, ...

2.1.36 optional

Description: optional variable: ?name?, ...

2.1.37 declaration

Description: item declaration: a list containing a class, command, ... declaration.

2.1.38 date

Description: date: Fri Feb 03 19:09:35 WET 1984, ...

2.1.39 choice

Description: choice list: {one two three}, ...

2.1.40 any

Description: any value.

2.1.41 boolflag

Description: boolean flag.

2.1.42 url

Description: URL: http://wiki.tcl.tk.

2.2 Options

None.

2.3 Namespaces

  1. ::Simple::Type

  2. ::Simple::Type::Priv

2.4 Commands

2.4.1 ::Simple::Type::assert-is type value ?extra? ?description?

Synopsis: throws an error if a value does not conform to a type.

Details: see section 4.1.

2.4.2 ::Simple::Type::cget

Synopsis: gets the package options.

Details: see section 4.2.

2.4.3 ::Simple::Type::configure

Synopsis: configures the package options.

Details: see section 4.3.

2.4.4 ::Simple::Type::convert binary integer

Synopsis: converts an integer in any base to binary.

Details: see section 4.4.

2.4.5 ::Simple::Type::convert boolean extBoolean

Synopsis: converts an extended boolean to boolean.

Details: see section 4.5.

2.4.6 ::Simple::Type::convert decimal integer

Synopsis: converts an integer in any base to decimal.

Details: see section 4.6.

2.4.7 ::Simple::Type::convert flag flag

Synopsis: converts a flag to an unqualified name.

Details: see section 4.7.

2.4.8 ::Simple::Type::convert hexadecimal integer

Synopsis: converts an integer in any base to hexadecimal.

Details: see section 4.8.

2.4.9 ::Simple::Type::convert octal integer

Synopsis: converts an integer in any base to octal.

Details: see section 4.9.

2.4.10 ::Simple::Type::convert optional optional

Synopsis: converts an optional value to an unqualified name.

Details: see section 4.10.

2.4.11 ::Simple::Type::declare type ?-description string? ?-matchingscript script?

Synopsis: declares a type.

Details: see section 4.11.

2.4.12 ::Simple::Type::delete type

Synopsis: deletes a type.

Details: see section 4.12.

2.4.13 ::Simple::Type::information declared ?typePattern?

Synopsis: returns the list of declared types.

Details: see section 4.13.

2.4.14 ::Simple::Type::information description type

Synopsis: returns a type description.

Details: see section 4.14.

2.4.15 ::Simple::Type::information exists type

Synopsis: returns whether a type exists.

Details: see section 4.15.

2.4.16 ::Simple::Type::information matchingscript type

Synopsis: returns a type matching script.

Details: see section 4.16.

2.4.17 ::Simple::Type::is type value ?extra?

Synopsis: returns whether a value conforms to a type.

Details: see section 4.17.

2.4.18 ::Simple::Type::modify description type description

Synopsis: modifies a type description.

Details: see section 4.18.

2.4.19 ::Simple::Type::modify matchingscript type matchingScript

Synopsis: modifies a type matching script.

Details: see section 4.19.

2.4.20 ::Simple::Type::outside-range range value

Synopsis: returns whether a numeric value is outside a numeric range.

Details: see section 4.20.

2.5 Errors

2.5.1 ::Simple::Type::BAD-DERIVED-TYPE

Message: invalid derived type "derived type".

Explanation: the derived type derived type is invalid. All basic types can be derived but "boolflag". The only derived range types valid are those in which the basic type is one of integer, float, decimal, hexadecimal, binary, octal, anybase or number.

2.5.2 ::Simple::Type::DERIVED-TYPE-OUT-OF-CONTEXT

Message: a derived type such as "derived type" is not valid in this context.

Explanation: derived type is a derived type, but these types are valid for the ::Simple::Type::is and ::Simple::Type::information exists procedures only.

2.5.3 ::Simple::Type::NEGATIVE-INT

Message: negative integer "integer".

Explanation: the integer integer is negative but a positive integer was expected.

2.5.4 ::Simple::Type::ALREADY-EXISTS

Message: type "type" already exists.

Explanation: type type could not be created because it already exists.

Corrective action: delete the type.

3. Package further information

3.1 Details

3.2 Todo

4. Package command details

4.1 ::Simple::Type::assert-is type value ?extra? ?description?

4.1.1 Command description

Synopsis: throws an error if a value does not conform to a type.

Access mode: public.

4.1.1.1 Overview

This procedure throws an error if a value does not conform to a type. The message uses the description, if given; otherwise, the type description is used, if it was stored when the type was created; otherwise, the type name is used.

4.1.2 Command public API

Arguments:

Argument Type Default value/ choices Description
type type (n/a) Type name
value any (n/a) Value or list of values
?extra? string (empty string) Extra data required to check the conformation
?description? string (empty string) Type description

Returns: error if the value does not conform to the type.

4.1.3 Command further information

4.1.3.1 Remarks

4.2 ::Simple::Type::cget

4.2.1 Command description

Synopsis: gets the package options.

Access mode: public.

4.2.2 Command public API

Arguments: none.

Returns: the requested option value or the whole list of options if none specified.

4.3 ::Simple::Type::configure

4.3.1 Command description

Synopsis: configures the package options.

Access mode: public.

4.3.2 Command public API

Arguments: none.

Returns: the "package with no options" error is thrown.

4.4 ::Simple::Type::convert binary integer

4.4.1 Command description

Synopsis: converts an integer in any base to binary.

Access mode: public.

Keywords: decimal, hexadecimal, binary, octal, base and convert.

4.4.1.1 Overview

Converts a decimal, hexadecimal, binary or octal integer to binary base. The resulting binary constant is of the form 0bbinary-digits. where binary-digits are a sequence of 0s and 1s.

4.4.2 Command public API

Arguments:

Argument Type Default value/ choices Description
integer anybase (n/a) Positive integer

Returns: the converted integer.

4.5 ::Simple::Type::convert boolean extBoolean

4.5.1 Command description

Synopsis: converts an extended boolean to boolean.

Access mode: public.

Keywords: boolean and convert.

4.5.1.1 Overview

This procedure converts from extended booleans (true/false, yes/no or on/off) to 1/0 because the Tcl expr and if commands can't use non-numeric string as operand of "!" (sic).

4.5.1.2 Examples

4.5.2 Command public API

Arguments:

Argument Type Default value/ choices Description
extBoolean extbool (n/a) Extended boolean

Returns: the converted boolean.

4.6 ::Simple::Type::convert decimal integer

4.6.1 Command description

Synopsis: converts an integer in any base to decimal.

Access mode: public.

Keywords: decimal, hexadecimal, binary, octal, base and convert.

4.6.1.1 Overview

Converts a decimal, hexadecimal, binary or octal integer to decimal base.

4.6.2 Command public API

Arguments:

Argument Type Default value/ choices Description
integer anybase (n/a) Positive integer

Returns: the converted integer.

4.7 ::Simple::Type::convert flag flag

4.7.1 Command description

Synopsis: converts a flag to an unqualified name.

Access mode: public.

4.7.2 Command public API

Arguments:

Argument Type Default value/ choices Description
flag flag (n/a) Flag

Returns: the flag name.

4.8 ::Simple::Type::convert hexadecimal integer

4.8.1 Command description

Synopsis: converts an integer in any base to hexadecimal.

Access mode: public.

Keywords: decimal, hexadecimal, binary, octal, base and convert.

4.8.1.1 Overview

Converts a decimal, hexadecimal, binary or octal integer to hexadecimal base. The resulting hexadecimal constant is of the form 0xhexadecimal-digits where hexadecimal-digits are a sequence of uppercase hexadecimal digits, that is, the "0x" prefix contains a lowercase "x" but the actual digits are uppercase. This is different from the result of the "%#X" format specifier which returns an uppercase "X".

4.8.2 Command public API

Arguments:

Argument Type Default value/ choices Description
integer anybase (n/a) Positive integer

Returns: the converted integer.

4.9 ::Simple::Type::convert octal integer

4.9.1 Command description

Synopsis: converts an integer in any base to octal.

Access mode: public.

Keywords: decimal, hexadecimal, binary, octal, base and convert.

4.9.1.1 Overview

Converts a decimal, hexadecimal, binary or octal integer to octal base.

4.9.2 Command public API

Arguments:

Argument Type Default value/ choices Description
integer anybase (n/a) Positive integer

Returns: the converted integer.

4.10 ::Simple::Type::convert optional optional

4.10.1 Command description

Synopsis: converts an optional value to an unqualified name.

Access mode: public.

4.10.2 Command public API

Arguments:

Argument Type Default value/ choices Description
optional optional (n/a) Optional value

Returns: the optional value name.

4.11 ::Simple::Type::declare type ?-description string? ?-matchingscript script?

4.11.1 Command description

Synopsis: declares a type.

Access mode: public.

4.11.1.1 Overview

This procedure is used to declare a new type.

A type is made of three elements:

  1. the type name,

  2. the type description and

  3. type matching script.

The type name is an all-lowercase string. The type matching script is a Tcl script used to check whether a value conforms to the type. It shall assume that the value to be checked is passed in the value variable, and shall return either 1 or 0 depending on whether the value conforms to the type or not; throwing an error is also a valid action for non-conforming values. It gets evaluated as a procedure, so use upvar and uplevel to access the current stack level if needed.

A type with no empty matching script may hold any value.

4.11.1.2 Examples

4.11.2 Command public API

Arguments:

Argument Type Default value/ choices Description
type type (n/a) Type name
?-description? string (empty string) Type description
?-matchingscript? script return 1 Type matching script

Returns: the empty string.

Effects:

4.11.3 Command further information

4.11.3.1 Remarks

4.12 ::Simple::Type::delete type

4.12.1 Command description

Synopsis: deletes a type.

Access mode: public.

4.12.2 Command public API

Arguments:

Argument Type Default value/ choices Description
type type (n/a) Type name

Returns: the empty string.

Effects:

4.13 ::Simple::Type::information declared ?typePattern?

4.13.1 Command description

Synopsis: returns the list of declared types.

Access mode: public.

4.13.1.1 Overview

This procedure returns the list of declared types matching the given pattern. This includes native The Simple Development Library types as well as others declared via the ::Simple::Type::declare procedure.

4.13.2 Command public API

Arguments:

Argument Type Default value/ choices Description
?typePattern? pattern (empty string) Pattern

Returns: the list of types matching the given pattern.

4.13.3 Command further information

4.13.3.1 Remarks

4.14 ::Simple::Type::information description type

4.14.1 Command description

Synopsis: returns a type description.

Access mode: public.

4.14.2 Command public API

Arguments:

Argument Type Default value/ choices Description
type type (n/a) Type name

Returns: the type description.

4.15 ::Simple::Type::information exists type

4.15.1 Command description

Synopsis: returns whether a type exists.

Access mode: public.

4.15.1.1 Overview

This procedure returns whether a type exists. This includes native The Simple Development Library types as well as others declared via the ::Simple::Type::declare procedure. For derived types, the procedure returns whether the basic type exists.

4.15.2 Command public API

Arguments:

Argument Type Default value/ choices Description
type type (n/a) Type name

Returns: whether the type exists.

4.16 ::Simple::Type::information matchingscript type

4.16.1 Command description

Synopsis: returns a type matching script.

Access mode: public.

4.16.2 Command public API

Arguments:

Argument Type Default value/ choices Description
type type (n/a) Type name

Returns: the type matching script.

4.17 ::Simple::Type::is type value ?extra?

4.17.1 Command description

Synopsis: returns whether a value conforms to a type.

Access mode: public.

Keywords: type.

4.17.1.1 Overview

This procedure returns whether a value conforms to a type.

If the type is a derived list type, the value is considered a list of values, and the procedure returns whether all of them conform to the basic type. Empty lists conform to all types.

If the type is a derived array type, the value is considered the name of an array of values in the calling scope, and the procedure returns whether all of them conform to the basic type. Non-existing arrays conform to all types.

If the type is a derived range type, the value is considered a range whose limits are of the basic type.

4.17.2 Command public API

Arguments:

Argument Type Default value/ choices Description
type type (n/a) Type name
value any (n/a) Value or list of values
?extra? string (empty string) Extra data required to check the conformation

Returns: whether the value conforms to the type.

4.17.3 Command further information

4.17.3.1 Remarks

4.18 ::Simple::Type::modify description type description

4.18.1 Command description

Synopsis: modifies a type description.

Access mode: public.

4.18.2 Command public API

Arguments:

Argument Type Default value/ choices Description
type type (n/a) Type name
description string (n/a) New description

Returns: the empty string.

4.19 ::Simple::Type::modify matchingscript type matchingScript

4.19.1 Command description

Synopsis: modifies a type matching script.

Access mode: public.

4.19.2 Command public API

Arguments:

Argument Type Default value/ choices Description
type type (n/a) Type name
matchingScript script (n/a) New matching script

Returns: the empty string.

Effects:

4.20 ::Simple::Type::outside-range range value

4.20.1 Command description

Synopsis: returns whether a numeric value is outside a numeric range.

Access mode: public.

4.20.2 Command public API

Arguments:

Argument Type Default value/ choices Description
range number-range (n/a) Numeric range
value number (n/a) Numeric value

Returns: -1, 0 or 1, depending on whether the value is lower than the lower range limit, within the range or greater than the upper range limit.