3 ms·
In Dart, we merged tuples and records into a single construct. A record can have positional fields, named fields, or both. A record type can appear anywhere a t
by munificent 1y ago
In Dart, we merged tuples and records into a single construct. A record can have positional fields, named fields, or both. A record type can appear anywhere a type annotation is allowed. So in Dart these are both fine:
(int, int) can() => (1, 2);
({int sum, int product}) alsoCan() => (sum: 1, product: 2);
(int, {int remainder}) evenThis() => (1, remainder: 2);
The curly braces in the record type annotation distinguish the named fields from the positional ones. I don't love the syntax, but it's consistent with function parameter lists where the curly braces delimit the named parameters.
https://dart.dev/language/records https://dart.dev/language/records
- afiori 1y agoHow do you distinguish a tuple with both positional and named fields from a tuple that has a record as a field Like how do you write the type of (1, {sum:2}) ? Is it different from (1 , sum :2)?
- munificent 1y agoThe syntax is a little funny for mostly historical reasons. The curly braces are only part of the record type syntax. There's no ambiguity there because curly braces aren't used for anything else in type annotations (well, except for named parameters inside a function type's parameter list, but that's a different part of the grammar).
- afiori 1y agoso my examples would be (1, (sum:2)) and (1, sum:2)?
- munificent 1y agoYes, exactly.