Skip to content

Commit 51b9d7f

Browse files
authored
Merge branch 'main' into patch-3
2 parents c22afd4 + 1cbfd5f commit 51b9d7f

File tree

6 files changed

+50
-13
lines changed

6 files changed

+50
-13
lines changed

examples/language/test/records_test.dart

+23-2
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ void main() {
115115
test('record-multiple-returns', () {
116116
// #docregion record-multiple-returns
117117
// Returns multiple values in a record:
118-
(String, int) userInfo(Map<String, dynamic> json) {
118+
(String name, int age) userInfo(Map<String, dynamic> json) {
119119
return (json['name'] as String, json['age'] as int);
120120
}
121121

@@ -125,7 +125,7 @@ void main() {
125125
'color': 'blue',
126126
};
127127

128-
// Destructures using a record pattern:
128+
// Destructures using a record pattern with positional fields:
129129
var (name, age) = userInfo(json);
130130

131131
/* Equivalent to:
@@ -137,4 +137,25 @@ void main() {
137137
name;
138138
age;
139139
});
140+
141+
test('record-name-destructure', () {
142+
// #docregion record-name-destructure
143+
({String name, int age}) userInfo(Map<String, dynamic> json)
144+
// #enddocregion record-name-destructure
145+
{
146+
return (name: json['name'] as String, age: json['age'] as int);
147+
}
148+
149+
final json = <String, dynamic>{
150+
'name': 'Dash',
151+
'age': 10,
152+
'color': 'blue',
153+
};
154+
// #docregion record-name-destructure
155+
// Destructures using a record pattern with named fields:
156+
final (:name, :age) = userInfo(json);
157+
// #enddocregion record-name-destructure
158+
name;
159+
age;
160+
});
140161
}

src/content/language/concurrency.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ complete. A well-behaved app starts quickly, getting to the event loop as soon
280280
as possible. The app then responds to each queued event promptly, using
281281
asynchronous operations as necessary.
282282

283-
[async-await]: {{site.url}}/codelabs/async-await
283+
[async-await]: /codelabs/async-await
284284

285285
### The isolate life cycle
286286

src/content/language/extension-types.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -446,8 +446,9 @@ It's important to be aware of this quality when using extension types.
446446
Always keep in mind that an extension type exists and matters at compile time,
447447
but gets erased _during_ compilation.
448448

449-
An expression with the extension type `E` and the representation type `R`
450-
as its static type will be an object with type `R` at run time.
449+
For example, consider an expression `e` whose static type is the
450+
extension type `E`, and the representation type of `E` is `R`.
451+
Then, the run-time type of the value of `e` is a subtype of `R`.
451452
Even the type itself is erased;
452453
`List<E>` is exactly the same thing as `List<R>` at run time.
453454

src/content/language/patterns.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,8 @@ in a map. This section describes even more use cases, answering:
279279

280280
### Destructuring multiple returns
281281

282-
[Records][] allow aggregating and returning multiple values from a single function
283-
call. Patterns add the ability to destructure a record's fields
282+
Records allow aggregating and [returning multiple values][] from a single
283+
function call. Patterns add the ability to destructure a record's fields
284284
directly into local variables, inline with the function call.
285285

286286
Instead of individually declaring new local variables for each record field,
@@ -295,7 +295,8 @@ var age = info.$2;
295295

296296
You can destructure the fields of a record that a function returns into local
297297
variables using a [variable declaration](#variable-declaration) or
298-
[assigment pattern](#variable-assignment), and a record pattern as its subpattern:
298+
[assigment pattern](#variable-assignment), and a [record pattern][record]
299+
as its subpattern:
299300

300301
<?code-excerpt "language/lib/patterns/destructuring.dart (destructure-multiple-returns-2)"?>
301302
```dart
@@ -424,7 +425,7 @@ This case pattern simultaneously validates that:
424425
[collection literals]: /language/collections#control-flow-operators
425426
[null-assert pattern]: /language/pattern-types#null-assert
426427
[record]: /language/pattern-types#record
427-
[Records]: /language/records
428+
[returning multiple values]: /language/records#multiple-returns
428429
[refutable]: /resources/glossary#refutable-pattern
429430
[constant]: /language/pattern-types#constant
430431
[list]: /language/pattern-types#list

src/content/language/records.md

+17-3
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,12 @@ of their fields.
176176

177177
Records allow functions to return multiple values bundled together.
178178
To retrieve record values from a return,
179-
destructure the values into local variables using [pattern matching][pattern].
179+
[destructure][] the values into local variables using [pattern matching][pattern].
180180

181181
<?code-excerpt "language/test/records_test.dart (record-multiple-returns)"?>
182182
```dart
183183
// Returns multiple values in a record:
184-
(String, int) userInfo(Map<String, dynamic> json) {
184+
(String name, int age) userInfo(Map<String, dynamic> json) {
185185
return (json['name'] as String, json['age'] as int);
186186
}
187187
@@ -191,7 +191,7 @@ final json = <String, dynamic>{
191191
'color': 'blue',
192192
};
193193
194-
// Destructures using a record pattern:
194+
// Destructures using a record pattern with positional fields:
195195
var (name, age) = userInfo(json);
196196
197197
/* Equivalent to:
@@ -201,6 +201,18 @@ var (name, age) = userInfo(json);
201201
*/
202202
```
203203

204+
You can also destructure a record using its [named fields](#record-fields),
205+
using the colon `:` syntax, which you can read more about on the
206+
[Pattern types][] page:
207+
208+
<?code-excerpt "language/test/records_test.dart (record-name-destructure)"?>
209+
```dart
210+
({String name, int age}) userInfo(Map<String, dynamic> json)
211+
// ···
212+
// Destructures using a record pattern with named fields:
213+
final (:name, :age) = userInfo(json);
214+
```
215+
204216
You can return multiple values from a function without records,
205217
but other methods come with downsides.
206218
For example, creating a class is much more verbose, and using other collection
@@ -217,3 +229,5 @@ parallelization of futures of different types, which you can read about in the
217229
[pattern]: /language/patterns#destructuring-multiple-returns
218230
[`dart:async` documentation]: /libraries/dart-async#handling-errors-for-multiple-futures
219231
[parameters and arguments]: /language/functions#parameters
232+
[destructure]: /language/patterns#destructuring
233+
[Pattern types]: /language/pattern-types#record

src/content/tutorials/server/fetch-data.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ void main() async {
244244

245245
This results in the following JSON-formatted output,
246246
which can also be seen in your browser at
247-
[{{site.url}}/f/packages/http.json][mock-http-json].
247+
[`/f/packages/http.json`][mock-http-json].
248248

249249
```json
250250
{

0 commit comments

Comments
 (0)