Overview
| Comment: | Version 16p adds a field to the `Proc` structure. It allows for the typing of both the return value and the arguments of procedures. Example: tcl proc {pt}decale { {pt}p; {double}dx ?= 0; {double}dy ?= 0 } { set {pt}q [expr {$p(x) + $dx}] [expr {$p(y) + $dy}] return $q } set {pt}a 1 2 decale $a 1 1 ;# positional decale $a {dy:}5 ;# positional, followed by labels decale {dy:}5 {p:}$a ;# labels only, in any order Arguments Fields: Each field becomes a local variable subject to its defined contract. Defaults: `?= v` or `?= [cmd]`—evaluated at each procedure call; it may reference preceding fields. Remainder: A final field, `{list.T}args`, collects any excess arguments, each of which is validated. Automatic `upvar`: `{var.T}n` binds `n` to a variable in the callers scope, with its current value being validated. Call-site labels: Positional arguments following a label are disallowed; providing the same field twice or using an unknown label will also result in an error. Exception: If the target field is a sum type, the label selects the specific variant (e.g., `ps {s:}abc`). Messages: These take forms such as `wrong # args: should be "decale p ?dx? ?dy?"` or `argument "dx": expected floating-point number but got "abc"`. Return Value Cast: `proc {T}name` casts the result of a standard `return` to type `T`. Errors, `break`, `return -level 2`, `tailcall`, and `yield` are not subject to this cast. Local annotation: `return {U}$x` simply attaches an annotation, which expires when the procedure terminates. There is, therefore, no type overlapping (or "cumulation" of types). Tags: When a return value is a sum type, `return {point:}$z` selects the specific variant, and a strict sum type (`-strict`) mandates it. Using a tag that is not a valid variant, or applying one to a return value that is not a sum type, results in an error. Introspection and compatibility `type proc name returns args {…} return T`—`info args` and `info default` can "see" the fields. A valid Tcl list remains a standard list of arguments. An initial attempt caused the `proc-old-5.4` test to fail (due to an unclosed brace); this has been fixed, and the standard error is now correctly returned. Performance cost: A call with 3 arguments takes 1.2 µs, compared to 0.6 µs for an equivalent standard procedure. The return-type cast incurs no overhead if the value is already of the correct type. See next commit for complements |
|---|---|
| Timelines: | family | ancestors | descendants | both | tip-764_tcl90 |
| Files: | files | file ages | folders |
| SHA3-256: |
1508c3d76ada99be76eda49f86311191 |
| User & Date: | Florentis on 2026-11-05 08:59:55.226 |
| Original Comment: | Version 16p adds a field to the `Proc` structure. It allows for the typing of both the return value and the arguments of procedures. Example: tcl proc {pt}decale { {pt}p; {double}dx ?= 0; {double}dy ?= 0 } { set {pt}q [expr {$p(x) + $dx}] [expr {$p(y) + $dy}] return $q } set {pt}a 1 2 decale $a 1 1 ;# positional decale $a {dy:}5 ;# positional, followed by labels decale {dy:}5 {p:}$a ;# labels only, in any order Arguments Fields: Each field becomes a local variable subject to its defined contract. Defaults: `?= v` or `?= [cmd]`—evaluated at each procedure call; it may reference preceding fields. Remainder: A final field, `{list.T}args`, collects any excess arguments, each of which is validated. Automatic `upvar`: `{var.T}n` binds `n` to a variable in the callers scope, with its current value being validated. Call-site labels: Positional arguments following a label are disallowed; providing the same field twice or using an unknown label will also result in an error. Exception: If the target field is a sum type, the label selects the specific variant (e.g., `ps {s:}abc`). Messages: These take forms such as `wrong # args: should be "decale p ?dx? ?dy?"` or `argument "dx": expected floating-point number but got "abc"`. Return Value Cast: `proc {T}name` casts the result of a standard `return` to type `T`. Errors, `break`, `return -level 2`, `tailcall`, and `yield` are not subject to this cast. Local annotation: `return {U}$x` simply attaches an annotation, which expires when the procedure terminates. There is, therefore, no type overlapping (or "cumulation" of types). Tags: When a return value is a sum type, `return {point:}$z` selects the specific variant, and a strict sum type (`-strict`) mandates it. Using a tag that is not a valid variant, or applying one to a return value that is not a sum type, results in an error. Introspection and compatibility `type proc name returns args {…} return T`—`info args` and `info default` can "see" the fields. A valid Tcl list remains a standard list of arguments. An initial attempt caused the `proc-old-5.4` test to fail (due to an unclosed brace); this has been fixed, and the standard error is now correctly returned. Performance cost: A call with 3 arguments takes 1.2 µs, compared to 0.6 µs for an equivalent standard procedure. The return-type cast incurs no overhead if the value is already of the correct type. |
| Other Links: | branch diff | manifest | tags |
Context
|
2026-11-05
| ||
| 09:32 | v16p: product field is optional if all its subfields have a default value; messages without a produc... check-in: 5e98c7c6b0 user: Florentis tags: tip-764_tcl90 | |
| 08:59 | Version 16p adds a field to the `Proc` structure. It allows for the typing of both the return value ... check-in: 1508c3d76a user: Florentis tags: tip-764_tcl90 | |
|
2026-11-04
| ||
| 10:46 | V16o 1. A variant whose type is a product is constructed via its fields. ``` set {segment.lien}s {c... check-in: f77a502eb6 user: Florentis tags: tip-764_tcl90 | |
Changes
Modified typeparse/REPRISE.md
from [072d9bace0]
to [97f2680152].
[diff]
Modified typeparse/VERSION
from [b2a0fc2ab1]
to [7801b7fd53].
[diff]
Modified typeparse/doc/764.md
from [19d0487499]
to [811ceb2e24].
[diff]
Modified typeparse/doc/ANNOTATIONS.fr.md
from [1f9c7f788c]
to [73dd99ae13].
[diff]
Modified typeparse/exemples/texte_enrichi/demo.tcl
from [96ad076923]
to [82aef9a2a0].
[diff]
Modified typeparse/noyau/LISEZMOI.md
from [1bd07d373c]
to [b9262933c9].
[diff]
Modified typeparse/noyau/ORDRE.txt
from [d35e9d84f0]
to [eaab775e2c].
[diff]
Added typeparse/noyau/procs-typees.patch version [dc7b2a5904].
Modified typeparse/tclTypes.c
from [61021ce527]
to [6384e8d135].
[diff]
Modified typeparse/tclTypes.h
from [ed6380dcda]
to [162776cf9e].
[diff]
Modified typeparse/tests_tclsh/t28_type_deref.tcl.out
from [ae4b2882b4]
to [cbcd12c108].
[diff]
Modified typeparse/tests_tclsh/t93_piste_b.out
from [170659572f]
to [6c4756fc34].
[diff]
Added typeparse/tests_tclsh/t96_procs_typees.out version [7a498f2f03].
Added typeparse/tests_tclsh/t96_procs_typees.tcl version [c61e09383f].