--- a/generic/tclTypedVar.c
+++ b/generic/tclTypedVar.c
@@ -429,6 +429,75 @@
return TCL_ERROR;
}
+/*
+ * Deux lectures possibles pour n == nf arguments (n pair) : positionnelle
+ * (une valeur par champ) ou nom/valeur (n/2 couples, champs omis par
+ * défaut). On essaie les deux SANS rien modifier ni laisser de message :
+ * si une seule est valide on la choisit ; si les deux le sont, c'est une
+ * ambiguïté réelle ; si aucune, on garde la lecture positionnelle (son
+ * message d'erreur est celui d'avant).
+ */
+static int
+ProductTryPositional(Tcl_Interp *interp, TclType *tp, Tcl_Size n,
+ Tcl_Obj *const a[])
+{
+ Tcl_Size i;
+ Tcl_InterpState st = Tcl_SaveInterpState(interp, TCL_OK);
+ int ok = 1;
+
+ for (i = 0; i < n && ok; i++) {
+ Tcl_Obj *c = TclTypedConform(interp, tp->fields[i].type, a[i]);
+
+ if (c == NULL) {
+ ok = 0;
+ } else {
+ TclDiscard(c);
+ }
+ }
+ (void) Tcl_RestoreInterpState(interp, st);
+ return ok;
+}
+
+static int
+ProductTryNameValue(Tcl_Interp *interp, TclType *tp, Tcl_Size n,
+ Tcl_Obj *const a[])
+{
+ Tcl_Size nf = tp->nfields, i, idx;
+ Tcl_InterpState st = Tcl_SaveInterpState(interp, TCL_OK);
+ int ok = 1;
+ char seen[256];
+
+ if (nf > (Tcl_Size) sizeof(seen)) {
+ (void) Tcl_RestoreInterpState(interp, st);
+ return 0;
+ }
+ memset(seen, 0, sizeof(seen));
+ for (i = 0; i < n / 2 && ok; i++) {
+ Tcl_Obj *c;
+
+ idx = TclTypeFieldIndex(tp, Tcl_GetString(a[2 * i]));
+ if (idx < 0 || seen[idx]) {
+ ok = 0;
+ break;
+ }
+ seen[idx] = 1;
+ c = TclTypedConform(interp, tp->fields[idx].type, a[2 * i + 1]);
+ if (c == NULL) {
+ ok = 0;
+ } else {
+ TclDiscard(c);
+ }
+ }
+ for (i = 0; i < nf && ok; i++) {
+ if (!seen[i] && tp->fields[i].defcmd == NULL
+ && tp->fields[i].defval == NULL) {
+ ok = 0; /* champ omis sans valeur par défaut */
+ }
+ }
+ (void) Tcl_RestoreInterpState(interp, st);
+ return ok;
+}
+
static int
BuildProduct(Tcl_Interp *interp, TclType *tp, Tcl_Size n, Tcl_Obj *const a[],
Tcl_Obj **contentPtr)
@@ -436,7 +505,7 @@
Tcl_Size nf = tp->nfields, i, idx;
Tcl_Obj **slot, *content;
TclType *at;
- int status, variant, rc = TCL_ERROR;
+ int status, variant, rc = TCL_ERROR, forceNameValue = 0;
slot = (Tcl_Obj **) Tcl_Alloc((nf ? nf : 1) * sizeof(Tcl_Obj *));
memset(slot, 0, (nf ? nf : 1) * sizeof(Tcl_Obj *));
@@ -447,7 +516,22 @@
rc = TCL_OK;
goto done;
}
- if (n == nf) {
+ if (n == nf && n >= 2 && n % 2 == 0) {
+ int pos = ProductTryPositional(interp, tp, n, a);
+ int nv = ProductTryNameValue(interp, tp, n, a);
+
+ if (pos && nv) {
+ Tcl_SetObjResult(interp, Tcl_ObjPrintf(
+ "ambiguous arguments for %s: they read both as %"
+ TCL_SIZE_MODIFIER "d positional values and as name/value "
+ "pairs; name every field (name value ...) or change the "
+ "first value", tp->name, nf));
+ Tcl_SetErrorCode(interp, "TCL", "TYPE", "AMBIGUOUS", (char *) NULL);
+ goto done;
+ }
+ forceNameValue = (!pos && nv);
+ }
+ if (n == nf && !forceNameValue) {
for (i = 0; i < nf; i++) {
slot[i] = TclTypedConform(interp, tp->fields[i].type, a[i]);
if (slot[i] == NULL) {