Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added constructor tear-offs #5850

Merged
merged 4 commits into from
May 22, 2024
Merged

Added constructor tear-offs #5850

merged 4 commits into from
May 22, 2024

Conversation

atsansone
Copy link
Contributor

Fixes #3577

@atsansone atsansone added the review.tech Awaiting Technical Review label May 20, 2024
@dart-github-bot
Copy link
Collaborator

dart-github-bot commented May 20, 2024

Visit the preview URL for this PR (updated for commit 9ce05e2):

https://dart-dev--pr5850-fix-3577-goq9px4j.web.app

Copy link
Contributor

@craiglabenz craiglabenz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good overall, but left a few thoughts.


```dart tag=bad
// Instead of a lambda for a named constructor:
var strings = charCodes.map ((code) => String.fromCharCode (code));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There seems to be an extra space here between String.fromCharCode and (code).

var strings = charCodes.map ((code) => String.fromCharCode (code));

// Instead of a lambda for an unnamed constructor:
var buffers = charCodes.map ((code) => StringBuffer (code));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same extra space issue here.

var buffers = charCodes.map ((code) => StringBuffer (code));
```

For visual learners, watch this Decoding Flutter video on tear-offs.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or masochists

You might assign an anonymous function to a variable so that,
for example, you can add or remove it from a collection.
Though you name most functions, such as `main()` or `printElement()`.
you can create nameless functions.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this clause is missing the "also" from the before version.

"...you can also create nameless functions."

for example, you can add or remove it from a collection.
Though you name most functions, such as `main()` or `printElement()`.
you can create nameless functions.
These functions are called _anonymous function_, _lambda_, or _closure_.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these all be plural?

Then in the anonymous function passed to `forEach`,
each converted string is printed out alongside its length.
Then, the anonymous function passed to `forEach`,
prints each converted string with its length.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know the ensuing sample predates this PR and was potentially outside the scope of what you wanted to get into, but it may be worth noting that the code this updated sentence describes directly conflicts with a Dart style guide recommendation: https://dart.dev/tools/linter-rules/avoid_function_literals_in_foreach_calls

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Took a moment to build the example.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! Interestingly enough, I don't even like this particular lint and ignore it myself all the time. But, since it is a lint in the style guide, I figure our own docs might as well follow it.

Copy link
Contributor

@craiglabenz craiglabenz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@atsansone atsansone added st.RFM Ready to merge or land and removed review.tech Awaiting Technical Review labels May 22, 2024
@atsansone atsansone merged commit 28213df into dart-lang:main May 22, 2024
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
st.RFM Ready to merge or land
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Document constructor tear-offs
3 participants