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

proposal: use_of_instead_of_from for collections. #57106

Open
kevmoo opened this issue Jul 4, 2024 · 11 comments
Open

proposal: use_of_instead_of_from for collections. #57106

kevmoo opened this issue Jul 4, 2024 · 11 comments
Labels
analyzer-linter Issues with the analyzer's support for the linter package area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. linter-lint-proposal linter-status-pending type-enhancement A request for a change that isn't a bug

Comments

@kevmoo
Copy link
Member

kevmoo commented Jul 4, 2024

use_of_instead_of_from

Description

The from ctor on collections is more expensive. It does casts. If possible, use the of constructor.

Details

If the source collection has a known, compatible type statically, of should be used.

Kind

Performance.

Bad Examples

final slowList = List<int>.from(someIterableOfInt);

Good Examples

final fastList = List<int>.of(someIterableOfInt);

Discussion

This should apply to List, Set, Map and related subtypes.

See 6fae002

@mkustermann
Copy link
Member

I'd rather just deprecate & remove the *from constructors, see #52400

@srawlins
Copy link
Member

srawlins commented Jul 5, 2024

If the from constructors are marked @deprecated, then a "data-driven fix" (documentation link missing) can inform dart fix to convert from one constructor to another.

@lrhn
Copy link
Member

lrhn commented Jul 6, 2024

Agree on just deprecating the old APIs.

I don't want to have lint's for every API preference. There should be a good reason to make people avoid an existing non-deprecated API.
And if the API is really bad, or should be deprecated, rather than spend resources on building a lint.

@kevmoo
Copy link
Member Author

kevmoo commented Jul 6, 2024

@lrhn for some nuance, I'm not saying deprecate the old API. I'm just saying don't use from if you're going (statically) from T to T.

The argument for deprecating from is pretty solid, I guess. But it's a seperate argument.

@mkustermann
Copy link
Member

The argument for deprecating from is pretty solid, I guess. But it's a seperate argument.

@kevmoo Once it's deprecated - there's no longer a need for a new lint, is there?

It's always better to make developers use the good path by making it the only path - instead of providing many knobs/hints/... - the fact that we have a need for so many hints (see here) is IMHO a bad smell.

@kevmoo
Copy link
Member Author

kevmoo commented Jul 8, 2024

@lrhn @mkustermann – then let's do it. What's the recommended alternative for folks using from if they can't use of?

Iterable.cast<T>?

@lrhn
Copy link
Member

lrhn commented Jul 8, 2024

Literal. [for (var v in iterable) v as T].
That makes the cast explicit.
(And instead of List.of, just use [... iterable], it's even shorter than the constructor.)

Using List.of(iterable.cast<T>()) is also an option.

@munificent
Copy link
Member

I was going to suggest we keep List.from() since it's the only way I could think of to create a list:

  1. From another list.
  2. With a different element type (by casting each element).
  3. That is non-growable.

But I realize that in the rare cases where you want all three of these, you could always use List.filled() or List.generate().

So +1 for getting rid of List.from().

@natebosch
Copy link
Member

it's the only way I could think of to create a list:

I think it's more clear to use List<Foo>.of(iterable.cast()) than List.filled or List.generate for this case.

+1 for deprecating .from(). There is a lot of usage, but from a skim I don't see any cases that would be harmed by a refactor.

@rakudrama
Copy link
Member

I would also like to see the .unmodifiable constructors be more strongly typed.
In the pattern of constructor names that our users have come to expect, there is no constructor for ?:

.from : .of :: .unmodifiable : ?

If we are deprecating from, can we 'just' make .unmodifiable stronger rather than invent a new clunky name for what we really want?

@lrhn
Copy link
Member

lrhn commented Jul 15, 2024

I'd love to make those better typed too. It's breaking. Don't know how much. (Will check!)

Seems Set.unmodifiable is already typed well, it's only List and Map that needs it (you can see which types are older).

@srawlins srawlins added the type-enhancement A request for a change that isn't a bug label Jul 18, 2024
@devoncarew devoncarew transferred this issue from dart-lang/linter Nov 15, 2024
@devoncarew devoncarew added analyzer-linter Issues with the analyzer's support for the linter package area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. labels Nov 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analyzer-linter Issues with the analyzer's support for the linter package area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. linter-lint-proposal linter-status-pending type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

8 participants