TIP 764 is a proposal to add Annotations to tcl values.
An annotation is written in a source as braced-prefix of a word. {*}, the expand prefix operator is not taken as an annotation. An annotation can be retrieved by the command note get. Ex :
set word {annotation}Value
note get $word; # annotationThis annotation can be used for any purpose. Techically; $word is an annotated Tcl_obj, a new Tcl_obj Type, made of 2 Tcl_obj, one for the annotation, one for the value.
In the context of a var constructor, an annotation set on a varname is taken as a type.
set {int}i 4
type get $i; # int
note get $i; # ""The type of a value is not a note. Internally, the annotation object can contains two things : at most one type, and a set of notes.
set w {annotation}$i; #4
note get $w; # annotation
type get $w; # intTypes must be declared. An unknown type gives an error message :
set {qwerty}B hello; # unknown type "qwerty"Some basic types are included.
It is possible to compose a new type, from known types, using a DSL, the « type language », and the command type def.
Examples
# Define a product type :
type def Point {
{double}x;
{double}y;
{double}z
}
set {Point}P 1 2 3; # 1.0 2.0 3.0
set P; # 1.0 2.0 3.0
# Define a tagged union :
type def num {
{double}d , {int}i
}
set {num}D {d:}1.3
# a tagged union must be called with its variant field :
type variant $D; # d
set D(d); 1.3
set D([type variant $D]) ;# 1.3
set D; # d 1.3
# Define an Enumeration :
type def colors {"red" | "green" | "blue"}
set {colors}C yellow; # value "yellow" matches no variant of colors
# Define submembers :
type def biPoint {
{{double}x; {double}y; {double}z}A;
{{double}x; {double}y; {double}z}B
}
set {biPoint}AB {1 2 3} {3 2 1}; # {1.0 2.0 3.0} {3.0 2.0 1.0}
set AB; # {1.0 2.0 3.0} {3.0 2.0 1.0}TIP 764 is introducing a generalized array-like notation to access members of a typed value :
This path language is available :
Let's take an example with the bipoint type above :
set AB(A); # 1.0 2.0 3.0 (= point A)
set AB(A.y); # 2.0 ( = field y of A)
set AB(A.(x,z)); # 1.0 3.0 (= field x and z of A)
set AB((A,B).z); # 3.0 1.0 (field z of A and B)
set AB((A,B).(x,z)); # {1.0 3.0} {3.0 1.0} (= field x and z of A and B)
set AB(*); # {1.0 2.0 3.0} {3.0 2.0 1.0} (everything)
set AB(A.*); {1.0 2.0 3.0} (everything of field A)If we have a Matrix type :
type def Matrix list(3).list(3).double
set {Matrix}M {1 2 3} {4 5 6} {7 8 9}; # {1.0 2.0 3.0} {4.0 5.0 6.0} {7.0 8.0 9.0}
# first colonne
set M((0,1,2).0); # 1.0 4.0 7.0
# Transposed matrix
set tM $M( (0,1,2).0, (0,1,2).1, (0,1,2).2 ); # {1.0 4.0 7.0} {2.0 5.0 8.0} {3.0 6.0 9.0}