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.
40 lines
1.5 KiB
Plaintext
40 lines
1.5 KiB
Plaintext
// Explicitly-typed tuple construction `Tuple(...).( ... )` — the `Tuple(...)`
|
|
// TYPE followed by a `.( ... )` initializer, exactly like `Point.{ ... }` for
|
|
// structs. Symmetric trio (mirrors structs `Point` / `Point.{...}` / `.{...}`):
|
|
// - tuple TYPE `Tuple(A, B)` (annotation / return / arg)
|
|
// - anonymous VALUE `.(a, b)` (contextually typed)
|
|
// - typed VALUE `Tuple(A, B).(a, b)` (explicit type + initializer)
|
|
// A `Tuple(...).(...)` value equals the anonymous `.(...)` against that type.
|
|
// Named forms keep `:` in the type and `=` in the value.
|
|
|
|
#import "modules/std.sx";
|
|
|
|
// A `-> Tuple(i64, i64)` return type with a `.(b, a)` body.
|
|
swap :: (a: i64, b: i64) -> Tuple(i64, i64) {
|
|
.(b, a)
|
|
}
|
|
|
|
main :: () -> i32 {
|
|
// Annotation + anonymous value.
|
|
t : Tuple(i64, i64) = .(1, 2);
|
|
print("t = {} {}\n", t.0, t.1); // t = 1 2
|
|
|
|
// Explicitly-typed construction — same value as `.(3, 4)` against the type.
|
|
u := Tuple(i64, i64).(3, 4);
|
|
print("u = {} {}\n", u.0, u.1); // u = 3 4
|
|
|
|
// Named: annotation + value uses `=` for the value fields.
|
|
p : Tuple(x: i64, y: i64) = .(x = 5, y = 6);
|
|
print("p = {} {}\n", p.x, p.y); // p = 5 6
|
|
|
|
// Named: explicitly-typed construction.
|
|
q := Tuple(x: i64, y: i64).(x = 7, y = 8);
|
|
print("q = {} {}\n", q.x, q.y); // q = 7 8
|
|
|
|
// Function returning a tuple via a `.(b, a)` body.
|
|
s := swap(10, 20);
|
|
print("s = {} {}\n", s.0, s.1); // s = 20 10
|
|
|
|
0
|
|
}
|