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

Clarify when to use Part directives #2371

Closed
felangel opened this issue Apr 25, 2020 · 9 comments · Fixed by #5932
Closed

Clarify when to use Part directives #2371

felangel opened this issue Apr 25, 2020 · 9 comments · Fixed by #5932
Assignees
Labels
a.libraries Relating to the Dart standard libraries. act.wait-for-customer Needs response from customer e1-hours Can complete in < 8 hours of normal, not dedicated, work p3-low Valid but not urgent concern. Resolve when possible. Encourage upvote to surface.

Comments

@felangel
Copy link

felangel commented Apr 25, 2020

Can you please clarify what is meant by:

Note: You may have heard of the part directive, which allows you to split a library into multiple Dart files. We recommend that you avoid using part and create mini libraries instead.

https://dart.dev/guides/libraries/create-library-packages#organizing-a-library-package

It’s a bit unclear if part usage is discouraged in general or just in the context of library creation and if so it would be nice to elaborate on the rationale. Happy to open a PR to improve the docs once this has been clarified. Thanks!

@kwalrath
Copy link
Contributor

My understanding is that part was an early language feature that fell out of common use, except in automatically generated code. Also, the relatively new extension feature can be used in some places where part used to be necessary.

Does that sound right, @kevmoo or @munificent? How can we clarify our guidance?

@kwalrath kwalrath added the act.wait-for-customer Needs response from customer label Apr 27, 2020
@kevmoo
Copy link
Member

kevmoo commented Apr 27, 2020 via email

@munificent
Copy link
Member

I agree with Kevin. Maybe say:

Note: You may have heard of the part directive, which allows you to split a library into multiple Dart files. Part files are useful for incorporating generated code into a library, but we otherwise recommend that you don't use them.

@felangel
Copy link
Author

felangel commented Apr 27, 2020

Thanks @kwalrath, @kevmoo, and @munificent for the clarification! Is there some justification for avoiding parts outside of code generation? I feel there are cases where parts are still very useful. Take the following scenario for example:

class Foo {...}

class Bar {...}

class MyClass extends Base<Foo, Bar>{...}

We can define Foo and Bar alongside MyClass which is fine but might lead to a somewhat bloated file.

Alternatively, we can define Foo and Bar separately and import them into my_class.dart but we then either need to re-export them, introduce a "barrel file", or use multiple imports to access Foo, Bar, and MyClass from elsewhere in the code.

I find parts are useful for this sort of scenario because they allow me to separate the implementations of these individual components while still representing them as parts of the whole MyClass.

part 'foo.dart';
part 'bar.dart';

class MyClass extends Base<Foo, Bar> {...}

I can then access Foo, Bar, and MyClass via a single import and it also helps when code generation is applied to Foo, Bar, or MyClass because a single .g.dart file is generated as opposed to 3.

Is there anything "wrong" with this approach? Thanks 🙏

@jorgecoca
Copy link

The case that @felangel described is what is being used in the flutter_bloc IDE extensions, and I personally like it a lot, since it makes the integration so much easier, and we do not need a barrel file just for the bloc (+ events and states).

@munificent
Copy link
Member

The main problems is that those part files:

  • Share the same private scope as the library and all other parts.
  • Can't contain their own imports.

So you don't get the benefits of being able to reason about each file independently. They are deeply intertwined. It only really lets you break your file into smaller files textually but not conceptually. In fact, it can end up more confusing because the separate files lead you to think things are more encapsulated than they actually are.

Because of that, our experience is that for most users parts don't carry their weight as a feature.

@atsansone atsansone added this to the Next Major Release milestone Apr 4, 2023
@atsansone atsansone added the a.libraries Relating to the Dart standard libraries. label Apr 5, 2023
@atsansone atsansone changed the title Clarification on Part Usage Clarify when to use Part directives Apr 5, 2023
@atsansone atsansone added p3-low Valid but not urgent concern. Resolve when possible. Encourage upvote to surface. e1-hours Can complete in < 8 hours of normal, not dedicated, work ltw-triage labels Apr 5, 2023
@atsansone
Copy link
Contributor

@munificent : Is this still a going concern?

@munificent
Copy link
Member

I think this comment is still decent guidance. We don't really want users hand authoring part files. They're much better off using separate libraries and using export to compose them. But part files are widely used for generated code.

@minseok-jeong-gn
Copy link

For 2024 my use,
Just use part for code generation others none.

@atsansone atsansone self-assigned this May 15, 2024
atsansone added a commit to atsansone/site-www that referenced this issue Jul 18, 2024
Fixes dart-lang#2371

---------

Co-authored-by: Marya <111139605+MaryaBelanger@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a.libraries Relating to the Dart standard libraries. act.wait-for-customer Needs response from customer e1-hours Can complete in < 8 hours of normal, not dedicated, work p3-low Valid but not urgent concern. Resolve when possible. Encourage upvote to surface.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants