Artifact d6d585f9fda6ffe499c6dcf1903663dc6adc41e725b0306d1275966b4921ad69:
- File typeparse/tests_tclsh/t67_conversion_variantes.tcl — part of check-in [582865c204] at 2026-10-23 11:20:46 on branch tip-764_tcl90 — Conversion rules between variants are now in place: this is version v15n, found in typeparse.zip. tcl type def temp {{double}celsius, {double}fahrenheit} type convert temp.celsius temp.fahrenheit {{x} {expr {$x*9/5.0 + 32}}} set {temp}t {celsius}100 set t(fahrenheit) ;# 212.0 set u {temp.fahrenheit}$t ;# 212.0, of type temp.fahrenheit type cast temp.fahrenheit $t ;# same Reading an inactive variant, or casting to {S.b}: If a rule exists, it is applied—even between variants with different underlying forms. For variants with the same form but no defined rule, an error occurs: "the current variant of temp is 'celsius', not 'fahrenheit'; both have the same form, so reading one as the other needs a rule: type convert temp.celsius temp.fahrenheit {{x} ...}." Otherwise, it defaults to a standard cast, as in v15k: reading {i}5 via $v(s) yields 5. This resolves the question left open in v15k regarding the reading of an inactive variant. Limitations, as previously agreed: Rules do not apply during type inference (e.g., `set {temp}y 5` remains ambiguous), nor during assignment (e.g., `set t {fahrenheit}50` changes the variant without performing a conversion). Rules cannot be chained, and each direction must be explicitly declared. `type convert` will reject cases involving two different sum types, an unknown variant, the same variant specified twice, or a mix of a variant and a simple (non-sum) type. For a sum type with a selector S($field), converted reads work, but the selector field itself cannot be modified via this mechanism. Two fixes included: Previously, `{temp.fahrenheit}$t` would ignore the requested variant and leave the value as `temp.celsius`. `type cast S.v` … previously resulted in an "unknown type 'v'" error. Both now work. Tests against v15: No new discrepancies; all expected `.out` outputs pass. Valgrind reports no issues with the new test cases. New test: `t67_conversion_variantes`. I have documented everything in `doc/ANNOTATIONS.fr.md` (section 8 and the path table) and in `noyau/LISEZMOI.md` (v15n). In the project plan, I have marked this item as complete, alongside the note regarding the current variant used for disambiguation. Update: Replace `tclTypes.c`, `tclTypes.h`, `tclTypePath.c`, and `tclAnnotObj.c` in the `generic/` directory, then run `make`. There is no kernel patch; `maj.sh` handles the update. (user: Florentis, size: 2054) [annotate] [blame] [check-ins using]
# t67 : règles de conversion entre variantes d'une même somme (type convert S.a S.b) ; # lecture d'une variante inactive : la règle, sinon le cast ordinaire, sinon (même # forme) une erreur qui propose la règle. proc t {l s} { set rc [catch {uplevel 1 $s} m]; puts [format "%-30s %s %s" $l $rc $m] } type def temp {{double}celsius, {double}fahrenheit} type def S {{int}i, {string}s} set {temp}t {celsius}100 t "sans règle" {set t(fahrenheit)} t "cast sans règle" {set u {temp.fahrenheit}$t} type convert temp.celsius temp.fahrenheit {{x} {expr {$x*9/5.0 + 32}}} type convert temp.fahrenheit temp.celsius {{x} {expr {($x - 32)*5/9.0}}} t "t(fahrenheit)" {list $t(fahrenheit) [type get $t(fahrenheit)]} t "t(celsius)" {set t(celsius)} t "cast" {set u {temp.fahrenheit}$t; list $u [type get $u]} t "cast retour" {set w {temp.celsius}$u; list $w [type get $w]} t "cast même variante" {set w {temp.celsius}$t; list $w [type get $w]} t "type cast" {set u [type cast temp.fahrenheit $t]; list $u [type get $u]} t "t inchangé" {list $t [type get $t]} t "écrire changer" {set {temp}x {celsius}10; set x {fahrenheit}50; list $x [type get $x]} t "déduction : ambigu" {set {temp}y 5} t "S i lu s (cast)" {set {S}v {i}5; list $v(s) [type get $v(s)]} t "S s lu i (cast)" {set {S}v {s}12; list $v(i) [type get $v(i)]} t "S s abc lu i" {set {S}v {s}abc; set v(i)} type convert S.s S.i {{x} {string length $x}} t "S s abc lu i, règle" {set {S}v {s}abc; set v(i)} t "règle qui rend faux" {type convert S.i S.s {{x} {return -code error "non"}}; set {S}v {i}5; set v(s)} t "erreur : sommes ≠" {type convert temp.celsius S.i {{x} {}}} t "erreur : variante ?" {type convert temp.celsius temp.kelvin {{x} {}}} t "erreur : même" {type convert temp.celsius temp.celsius {{x} {}}} t "erreur : mêlé" {type convert temp.celsius double {{x} {}}} t "liste de temp" {set {list.temp}L $t; set L(0.fahrenheit)}