Files
sx/examples/types/0119-types-tuple-values.sx
agra 989e18b760 feat: tuple syntax cutover — Tuple(...) type + .(...) value
Replace the bare-paren tuple grammar with explicit, position-unambiguous
forms, mirroring how structs work:

  type     `(A, B)`        -> `Tuple(A, B)`          (named keeps `:`)
  value    `(a, b)`        -> `.(a, b)`              (named uses `=`)
  typed    (new)           -> `Tuple(A, B).(a, b)`   (like `Point.{...}`)
  failable `-> (T, !)`     -> `-> T !`
           `-> (T1, T2, !)`-> `-> Tuple(T1, T2) !`   (channel outside Tuple)

Bare `(...)` is now grouping only, everywhere; a comma in bare parens is a
hard error with a migration hint. Grouping, function types `(A, B) -> R`,
param lists, lambdas, and match bindings are unaffected.

`Tuple(...)` is strictly a TYPE in every position (including `size_of` /
`type_info` args); a tuple VALUE comes only from `.(...)` (anonymous) or
`Tuple(...).(...)` (explicitly typed). A bare `Tuple(1, 2)` is a tuple
type with non-type elements -> rejected.

The ~110 tuple-bearing corpus files were migrated with a one-shot
AST-aware migrator (the `sx migrate` tool from the prior commit, removed
here). New examples: 0130 (new syntax), 0131 (typed construction), 1060
(named-tuple failable return). 1116 golden updated for the new hint text.
2026-06-25 17:53:57 +03:00

56 lines
1.9 KiB
Plaintext

// Tuple values: construction, element access, struct-field storage,
// return, and operators. Regression for the tuple-construction bug where
// an inferred `:=` tuple literal lowered its element values under the
// enclosing fn's (narrower) return `target_type`, mismatching the
// independently-inferred i64 field types and yielding garbage on read.
#import "modules/std.sx";
Box :: struct { xs: Tuple(i32, i32); }
swap :: (a: i64, b: i64) -> Tuple(i64, i64) { .(b, a) }
fst :: (t: Tuple(i64, i64)) -> i64 { t.0 }
main :: () -> i32 {
// Inferred positional tuple + numeric field access.
pair := .(40, 2);
print("pair {} {}\n", pair.0, pair.1);
// Named tuple: named + numeric access.
named := .(x = 10, y = 20);
print("named {} {} {}\n", named.x, named.0, named.1);
// Element into a typed local (access path, not just print).
a : i64 = pair.0;
b : i64 = pair.1;
print("locals {} {}\n", a, b);
// Tuple-typed struct field: store a tuple value, read both elements.
box : Box = ---;
box.xs = .(7, 9);
print("field {} {}\n", box.xs.0, box.xs.1);
// Return a tuple from a function.
s := swap(1, 2);
print("ret {} {}\n", s.0, s.1);
// Pass a tuple by value.
print("pass {}\n", fst(.(11, 22)));
// Operators: equality, concatenation, repetition, membership, lex.
print("eq {}\n", .(1, 2) == .(1, 2));
c := .(1, 2) + .(3, 4);
print("concat {} {}\n", c.0, c.3);
r := .(1, 2) * 3;
print("rep {} {}\n", r.0, r.5);
print("mem {}\n", 3 in .(1, 2, 3));
print("lex {}\n", .(1, 2) < .(1, 3));
// Mixed-size fields: a tuple with both an i64 and a string (16-byte fat
// pointer). Field types are tracked per-position, so reading each back is
// typed correctly (i64 prints as a number, string as text).
mixed := .(42, "hi");
print("mixed {} {}\n", mixed.0, mixed.1);
0
}