Skip to content

Commit

Permalink
Fixed code excerpts
Browse files Browse the repository at this point in the history
  • Loading branch information
atsansone committed May 21, 2024
1 parent 5d888c7 commit 9980a0d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 15 deletions.
9 changes: 5 additions & 4 deletions src/content/effective-dart/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -869,11 +869,12 @@ void main() {

{% render 'linter-rule-mention.md', rules:'unnecessary_lambdas' %}

When you refer to a function, method, or named constructor but omit the
parentheses, Dart creates a _tear-off_a closure that takes the same
When you refer to a function, method, or named constructor without parentheses,
Dart creates a _tear-off_. This is a closure that takes the same
parameters as the function and invokes the underlying function when you call it.
If all you need is a closure that invokes a named function with the same
parameters as the closure accepts, don't manually wrap the call in a lambda.
If your code needs a closure that invokes a named function with the same
parameters as the closure accepts, don't wrap the call in a lambda.
Use a tear-off.

<?code-excerpt "usage_good.dart (use-tear-off)"?>
```dart tag=good
Expand Down
4 changes: 2 additions & 2 deletions src/content/language/constructors.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,10 @@ var buffers = charCodes.map(StringBuffer.new);

```dart tag=bad
// Instead of a lambda for a named constructor:
var strings = charCodes.map ((code) => String.fromCharCode (code));
var strings = charCodes.map((code) => String.fromCharCode(code));
// Instead of a lambda for an unnamed constructor:
var buffers = charCodes.map ((code) => StringBuffer (code));
var buffers = charCodes.map((code) => StringBuffer(code));
```

For visual learners, watch this Decoding Flutter video on tear-offs.
Expand Down
20 changes: 11 additions & 9 deletions src/content/language/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,11 @@ prints each converted string with its length.
```dart
const list = ['apples', 'bananas', 'oranges'];
var uppercaseList = list.map((item) => item.toUpperCase()).toList();
var uppercaseList = list.map((item) {
return item.toUpperCase();
}).toList();
// Convert to list after mapping
for (var item in uppercaseList) {
print('$item: ${item.length}');
}
Expand All @@ -306,10 +308,10 @@ void main() {
const list = ['apples', 'bananas', 'oranges'];
var uppercaseList = list.map((item) {
return item.toUpperCase();
return item.toUpperCase();
}).toList();
// Convert to list after mapping
for (var item in uppercaseList) {
print('$item: ${item.length}');
}
Expand All @@ -323,8 +325,8 @@ to verify that it is functionally equivalent.

<?code-excerpt "misc/test/language_tour/functions_test.dart (anon-func)"?>
```dart
var uppercaseList = list.map((item) => item.toUpperCase()).toList();
uppercaseList.forEach((item) => print('$item: ${item.length}'));
var uppercaseList = list.map((item) => item.toUpperCase()).toList();
uppercaseList.forEach((item) => print('$item: ${item.length}'));
```

## Lexical scope
Expand Down Expand Up @@ -398,13 +400,13 @@ If your code needs a closure that invokes a named function with the same
parameters as the closure accepts, don't wrap the call in a lambda.
Use a tear-off.

<?code-excerpt "misc/lib/language_tour/tear-offs.dart (variables)" ?>
<?code-excerpt "misc/lib/language_tour/tear_offs.dart (variables)" ?>
```dart
var charCodes = [68, 97, 114, 116];
var buffer = StringBuffer();
```

<?code-excerpt "misc/lib/language_tour/tear-offs.dart (good-example)" ?>
<?code-excerpt "misc/lib/language_tour/tear_offs.dart (good-example)" ?>
```dart tag=good
// Function tear-off
charCodes.forEach(print);
Expand All @@ -413,7 +415,7 @@ charCodes.forEach(print);
charCodes.forEach(buffer.write);
```

<?code-excerpt "misc/lib/language_tour/tear-offs.dart (bad-example)" ?>
<?code-excerpt "misc/lib/language_tour/tear_offs.dart (bad-example)" ?>
```dart tag=bad
// Function lambda
charCodes.forEach((code) {
Expand Down

0 comments on commit 9980a0d

Please sign in to comment.