Skip to content
Tcl Source Code Artifact [0eb4dd4b11]
Login

Artifact [0eb4dd4b11]

Artifact 0eb4dd4b110a18ba5bc1ff7cc8dd1f4a77ef41f9e78a4bdd45ae84d3926db0cb:


# t143 (v18h) : l'annotation sur une valeur est une option, que la commande
#   qui reçoit le mot interprète. Un constructeur (set, argument typé,
#   déclaration, retour typé) y voit un type connu : une valeur nue le
#   prend, une valeur typée y est vérifiée, jamais convertie (type cast).
#   list garde l'annotation en note ; un paramètre non typé la reçoit comme
#   une note ; note set|add posent des notes (des données) ; annotate et
#   annotated rendent une annotation calculée ; type var dit le contrat.
proc t {nom script} {
    if {[catch {uplevel 1 $script} r]} {set r "ERREUR $r"}
    puts [format "%-34s %s" $nom $r]
}
proc tn {v} {list [type get $v] [note get $v]}
type def mm int; type def kg int; type def m double
type convert mm m {{{mm}x} {expr {$x/1000.0}}}
type def forme {{double}cote, {string}nom}
set {mm}t 1500

puts "== un constructeur : un type connu, vérifié, jamais converti"
t "valeur nue : prend le type"     {set y {mm}1500; tn $y}
t "double normalise"              {set y {m}1500; list $y [type get $y]}
t "valeur typée, même type"        {set y {mm}$t; tn $y}
t "valeur typée, la base"          {set y {int}$t; tn $y}
t "toute valeur est une chaîne"    {set y {string}$t; tn $y}
t "règle : refus"                  {set y {m}$t}
t "nominal sans lien : refus"      {set y {kg}$t}
t "troncature : refus"             {set {double}d 2.5; set y {int}$d}
t "nom inconnu : refus"            {set y {zz}5}
t "la conversion : type cast"      {tn [type cast m $t]}
t "argument typé, même règle"      {proc f {{m}x} {set x}; f {m}$t}
t "retour typé, même règle"        {proc {m}g {} {return $::t}; g}
t "type {T} : donner un type"      {tn [type {mm}1500]}
t "type {T} ne convertit pas"      {type {m}$t}

puts "== list garde l'annotation (une note)"
t "list {mm}1500"                  {tn [lindex [list {mm}1500] 0]}
t "list {zz}5"                     {tn [lindex [list {zz}5] 0]}
t "list {m}\$t : ni cast, ni erreur" {tn [lindex [list {m}$t] 0]}

puts "== un paramètre non typé : une option, lue comme note"
proc lit {v} {tn $v}
t "un type connu"                  {lit {mm}5}
t "un nom inconnu"                 {lit {zz}5}
t "une étiquette"                  {lit {/cote}2.0}
t "rangée dans une variable locale" {apply {{v} {set x $v; note get $x}} {/cote}2.0}
t "relais : annotated"             {apply {{v} {set {forme}S [annotated $v]; type variant $S}} {/cote}2.0}
t "relais : annotate"              {apply {{v} {set {forme}S [annotate [note get $v] $v]; type variant $S}} {/nom}2.0}
t "sans relais : ambigu"           {apply {{v} {set {forme}S $v}} {/cote}2.0}
t "paramètre typé : constructeur"  {apply {{{forme}v} {type variant $v}} {/cote}2.0}

puts "== annotate, annotated"
t "annotate T \$v = {T}\$v"          {set y [annotate mm 12]; tn $y}
t "annotate dans list : une note"  {tn [lindex [list [annotate mm 12]] 0]}
t "annotate, nom inconnu"          {set y [annotate zz 1]}
t "annotate {} : retirer le type"  {set y [annotate {} $t]; tn $y}
t "annotate list(2).int"           {set y [annotate list(2).int {1 2}]; type get $y}
t "le premier set reçoit"          {set tmp [annotate /cote 2.0]}

puts "== note set|add|get|has|delete : des données"
set v [note set {style.(gras,couleur=rouge),lien.cible="https://tcl.tk"} 5]
t "note set (syntaxe)"             {note get $v}
t "la valeur est inchangée"        {list $v [type get $v]}
t "note has (chemins)"             {list [note has $v style] [note has $v style.gras] [note has $v gras]}
t "note add"                       {note get [note add style.italique $v]}
t "la dernière l'emporte"          {note get [note add style $v]}
t "note delete (une branche)"      {note get [note delete $v style]}
t "rangée par set : une donnée"    {set y $v; note get $y}
t "dans un constructeur typé"      {set {int}yi $v; tn $yi}
t "sur une valeur typée"           {tn [note set zz $t]}
t "note set {} : plus de note"     {tn [note set {} [note set a 1]]}
t "syntaxe fausse"                 {note set "a,(" 1}
t "valeur citée"                   {note get [note set {a="x,y"} 1]}

puts "== type var : le type de la variable"
set {list.int}L 1 2
set libre $t
t "variable typée"                 {list [type var t] [type var L]}
t "variable libre, valeur typée"   {list [type var libre] [type get $libre]}
t "variable inconnue"              {type var nulle}

puts "== vues {list} {dict} : options de foreach, lassign..."
type def pt {{double}x; {double}y}
set {pt}p 1 2
t "foreach {list}\$p"              {set r {}; foreach {a b} {list}$p {lappend r $a $b}; set r}
t "lassign {list}\$p"              {lassign {list}$p a b; list $a $b}
t "dict for {dict}\$p"             {set r {}; dict for {k w} {dict}$p {lappend r $k}; set r}
t "set y {list}\$p : refus"        {set y {list}$p}
t "type cast list \$p"              {type get [type cast list $p]}

puts "== notes lues dans un texte : des données"
t "type parse"                     {set yp [type parse {{double@vitesse}5.0}]; set z $yp; tn $z}
t "élément d'une liste typée"      {set {list}P 5 [note set this 6]; set e [lindex $P 1]; note get $e}