How do you create a namespace for a Dart class? I come from a C# background, where one would just use namespace SampleNamespace { }
.
How do you achieve the same in Dart?
Classes are data types. They are an expanded concept of structures, they can contain data members, but they can also contain functions as members whereas a namespace is simply an abstract way of grouping items together. A namespace cannot be created as an object; think of it more as a naming convention.
Dart is an object-oriented language. It supports object-oriented programming features like classes, interfaces, etc. A class in terms of OOP is a blueprint for creating objects. A class encapsulates data for the object. Dart gives built-in support for this concept called class.
Modularity in Dart The Dart language was designed by keeping the modules in mind. Modularity in Dart is realized through packages, libraries, and classes. A library exposes functionality as a set of interfaces and hides the implementation from the rest of the world.
When you create a new library and use other libraries you want to make available automatically when using your package, then you use export : library mylib; export 'otherlib.dart'; // Definitions. You can use the show keyword to import/export only some parts of a library (like a class or something).
Dart doesn't have the concept of namespaces, but instead it has libraries. You can consider a library to be sort of equivalent to a namespace, in that a library can be made of multiple files, and contain multiple classes and functions.
Privacy in Dart is also at the library, rather than the class level (anything prefixed with an underscore is private to that library).
An example of defining a library (using the example of a utilities library:
// utilities.dart library utilities; // being the first statement in the library file
You can make other files part of the same library by using the part
keyword. Part files are only used to help organize your code; you can put all your classes in a single library file, or split them among several part files (or part files and the library file) - it has no effect on the execution. It is stylistic to put the main library file in a parent folder, and part files in a src/
folder.
Expanding the example to show Part files.
// utilities.dart library utilities; part "src/string_utils.dart"; part "src/date_utils.dart";
Those part files then link back to the library they are part of by using the part of
statement:
// src/string_utils.dart part of utilities; // functions and classes String reverseString(s) => // implementation .... String _stringBuilder(strings) => // a private (to the library) function, // indicated by the leading underscore //... snip other classes and functions
Now that you have a library containing a function, you can make use of that library elsewhere by import
ing the library:
// my_app.dart; import "path/to/library/utilities.dart"; main() { var reversed = reverseString("Foo"); // _stringBulider(["a","b"]); // won't work - this function is // only visible inside the library }
If you want to alias your library to avoid clashes (where you might import two libraries, both containing a reverseString()
function, you use the as
keyword:
// my_app.dart; import "path/to/library/utilities.dart"; import "some/other/utilities.dart" as your_utils; main() { var reversed = reverseString("Foo"); var your_reversed_string = your_utils.reverseString("Bar"); }
The import statement also makes use of packages, as imported by pub, Dart's package manager, so you can also host your library on github or elsewhere, and reference your library as so:
// my_app.dart; import "package:utilities/utilities.dart"; main() { var reversed = reverseString("Foo"); }
The pub dependency is defined in a pubspec.yaml
file, which tells pub where to find the library. You can find out more at pub.dartlang.org
It is important to note that only the library file can:
import
statements. Part files cannot.library
keyword. Part files cannot.part
files. Part files cannot.One final point is that a runnable app file can (and is likely to be) a library file, and can also be made of part files
// my_app.dart; library my_app; import "package:utilities/utilities.dart"; part "src/edit_ui.dart"; part "src/list_ui.dart"; part "src/foo.dart"; main() { var reversed = reverseString("Foo"); showEditUi(); // perhaps defined in edit_ui.dart....? }
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