Files
sx/examples/comptime/0623-comptime-metatype-tuple.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

24 lines
956 B
Plaintext

// Comptime TUPLE metaprogramming — `define` constructs a tuple and `type_info`
// reflects one, completing the reflect/construct triad (enum 0619, struct 0622,
// tuple here). Tuples are POSITIONAL, so `TupleInfo` is just a `[]Type` (no field
// names). Two paths:
// 1. Programmatic build: `define(declare("Pair"), .tuple(.{ elements = … }))`.
// 2. Round-trip: `define(declare("TripleCopy"), type_info((i64, bool, f64)))`
// reflects a source tuple type INTO a `.tuple(TupleInfo)` value and
// reconstructs it — no literal element list.
#import "modules/std.sx";
#import "modules/std/meta.sx";
Pair :: define(declare("Pair"), .tuple(.{ elements = .[ i64, f64 ] }));
TripleCopy :: define(declare("TripleCopy"), type_info(Tuple(i64, bool, f64)));
main :: () -> i32 {
p : Pair = .{ 3, 2.5 };
print("p = {} {}\n", p.0, p.1);
t : TripleCopy = .{ 7, true, 1.5 };
print("t = {} {} {}\n", t.0, t.1, t.2);
return 0;
}