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

后端 未结 2 1274
孤城傲影
孤城傲影 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: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.

提交回复
热议问题