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?
Dart provides the part keyword to allow you to split code into separate files in a library. It's used in the same file as the library keyword and needs to provide a relative path to the other source files that make up the library: for example, part "functions. dart"; . You can create new, empty text files for classes.
Dart has a rich set of core libraries that provide essentials for many everyday programming tasks such as working on collections of objects ( dart:collection ), making calculations ( dart:math ), and encoding/decoding data ( dart:convert ). Additional APIs are available in commonly used packages.
The Dart provides the import keyword, which is used to make the library available in the current file. We can use multiple libraries in a single file. For example - Dart built-in library URIs is used as dart scheme to refer to a library.
Dart uses a leading underscore in an identifier to mark members and top-level declarations as private. This trains users to associate a leading underscore with one of those kinds of declarations. They see “_” and think “private”.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With