What's the reasoning behind “part” and “part of” in Dart libraries?

后端 未结 2 1246
孤城傲影
孤城傲影 2021-01-17 08:46

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 stat

相关标签:
2条回答
  • 2021-01-17 09:22

    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?).

    0 讨论(0)
  • 2021-01-17 09:46

    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.

    0 讨论(0)
提交回复
热议问题