TIP 764

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; # annotation

This 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; # int

Languages of types

Types must be declared. An unknown type gives an error message :

set {qwerty}B hello; # unknown type "qwerty"

Some basic types are included.

Basic Types

  • scalar types :
    • int,
    • double,
    • string,
    • var
  • collections :
    • list
      • list(3) : 3 members
      • list.int : list of int
    • dict
      • dict(a,b,c) : dict with keys "a", "b" and "c".
        • dict(a,b ?= "hello") : dict with keys "a" and "b" which has a default value
      • list.dict
      • dict.list

Composition of types

It is possible to compose a new type, from known types, using a DSL, the « type language », and the command type def.

  • kind of composition :
    • product type : a list of fields, like a C-struct
    • sum type : a list of alternative field (variants), like in C union
    • Enumeration : a list of constant values
  • Rules of composition :
    • The syntax follows the annotation syntax : the type of a field is an annotation on its name.
      • The name of a field is allways on the right of a closed-brace.
      • the prefix of the field is between braces, and can be one sole type name or a type definition.
    • The binary operator ";" indicates a product field (struct)
    • The binary operator "," indicate a sum field whose alternatives (variants) can't be distinguished by value : tagged union
    • The binary operator "|" indicate a sum field whose alternatives can be distinguish by value : union
      • ";" "," and "|" can't be mixed at the same level of brace.

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}

Access to a member in a typed value

TIP 764 is introducing a generalized array-like notation to access members of a typed value :

  • any number (or end) is taken as an index of a list
  • any string is taken as the name of a field
  • "," binary operator is to collect members.
  • "." binary operator is to nest in depth
  • "(" and ")" allow to group elements.
  • space are allowed inside.

This path language is available :

  • in var substitution, with space allowed inside the index : $M( ....)
  • with readers of variables (set,lappend,...), but no space in the index is allowed in this case

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}