Proposal: TuplesMeanwhile, while we are waiting for any feedback :-) or for my tests to show any troubles with recent features/fixes, how do you feel about...
Tuples in FPC?I have been thinking about this for a while and I believe tuples would be a natural next step for FPC Unleashed. The core idea is simple: allow functions to return multiple values without forcing the programmer to declare a named record type every single time. Under the hood, a tuple is just an anonymous record - so we reuse the existing record infrastructure entirely. No new runtime concepts, no new memory layouts, just syntactic convenience backed by something the compiler already understands.
Here is what I have in mind.
1. Positional tuples as return typesThe simplest form. You list the types, the compiler generates an anonymous record with fields named _1, _2, _3, etc.
function divmod(a, b: Integer): (Integer, Integer);
begin
result._1 := a div b;
result._2 := a mod b;
end;
2. Named tuples as return typesSame thing, but you give the fields proper names. The syntax mirrors record field declarations.
function get_coords: (x, y: Integer);
begin
result.x := 10;
result.y := 20;
end;
3. Tuple literalsInstead of assigning fields one by one, you can assign the whole tuple at once. For positional tuples:
function divmod(a, b: Integer): (Integer, Integer);
begin
result := (a div b, a mod b);
end;
For named tuples, the delimiter is a comma (not semicolon - this is not a record declaration, it is a value expression). You can also reorder fields freely:
function get_coords: (x, y: Integer);
begin
result := (y: 20, x: 10); // any order
end;
Disambiguation with parenthesized expressions: a tuple literal must have 2 or more elements. (42) remains a plain arithmetic expression - no 1-element tuples.
4. Exit() with multiple valuesWhen a function returns a tuple and Exit() receives more than one argument, it wraps them into a tuple automatically. No extra parentheses needed:
function foo: (Integer, Integer);
begin
if condition then
Exit(0, 0); // not Exit((0, 0))
result := (100, 200);
end;
function bar: (a, b: Integer);
begin
Exit(a: 1, b: 2); // named exit
end;
5. Array of tupleTuples work as array element types, including with array literals:
var
points: array of (x, y: Integer);
pairs: array of (Integer, String);
begin
points := [(1, 2), (3, 4), (5, 6)];
end;
6. GenericsGeneric type parameters can appear inside tuples. Specialization produces a concrete anonymous record as usual:
function Zip<A, B>(xs: array of A; ys: array of B): array of (A, B);
7. Destructuring (already works in FPC Unleashed)FPC Unleashed already supports inline variable declarations with destructuring. Tuple returns would naturally work with this:
var (q, r) := divmod(17, 5);
// q = 3, r = 2
What we are NOT doing (at least not in the first version):- No tuples as parameter types. If you need multiple arguments, just declare multiple arguments - wrapping them in a tuple adds nothing.
- No structural compatibility between tuples and named records. This could be added later,
- No 1-element tuples. (42) is a parenthesized expression, period.
- No structural equality operator for tuples (yet).
Implementation strategyThe key principle: a tuple is an anonymous record. Nothing more.
(x, y: Integer) becomes an internal record with fields x and y of type Integer. (Integer, String) becomes an internal record with fields _1: Integer and _2: String.
This means:
- result.field access works automatically - it is a record.
- Per-field assignment works automatically.
- Managed types, RTTI, copy semantics - all free from the record infrastructure.
- Passing by value/const/var - already works for records.
The actual work is mostly in the parser: recognizing the tuple syntax in return type positions, generating the anonymous trecorddef with the right fields, and handling tuple literals as syntactic sugar for per-field assignment. The heaviest lifting is already done by the existing record machinery.
Thoughts? Objections? Things I am missing? A "no no", not Pascal-enough?