What's the reasoning behind “part” and “part o

2019-02-21 10:00发布

问题:

I was wondering what the reasoning is behind the way libraries and their content are defined. More specifically, a library needs to list all parts and the parts need to state the library of which they are part.

This bidirection seems unnecessary to me and I would expect that a reference from the library to the parts would be enough. Also, when adding or removing files from libraries, there are 2 places where modifications need to be made.

Can anyone explain this?

回答1:

I have not seen this specifically addressed anywhere, but I have wondered about this as well, and the conclusion I've come to is that it's a symptom of using library level privacy as opposed to class level privacy.

If a library only needed to list its parts then you could gain access to any library's internal properties simply by declaring it a part:

library hax;

part 'packages/somelib/secret.dart';

I now have access to any private field or method in secret.dart. I can do this with any third-party package I've imported, making the concept of privacy a joke.

Similarly, if only a part of declaration was required, any file could inject itself into a library by declaring that it was part of that library.

However, by requiring both a part declaration in the file declaring the library, and a part of declaration in the file that is to be included in the library, Dart avoids this situation.



回答2:

You can find some informations in this thread.

From Gilad Bracha :

Originally, parts were not tied to their libraries. People complained because it was difficult to give good tool support (i.e., what scope are you in when you open a part but not a library that uses it?).



标签: dart