Difference between revisions of "Conventional module name for importing all modules in a package"
m (class -> package) |
|||
(One intermediate revision by one other user not shown) | |||
Line 4: | Line 4: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | Since D doesn't | + | Since D doesn't support "*" imports (and it's unlikely that D would get such a shortcut), this capability could be added to a package by adding a module that imports the other modules. So what should such a module be called? There have been a few suggestions. |
These are common conventions: | These are common conventions: | ||
Line 12: | Line 12: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | == package.d == | ||
+ | |||
+ | Since several releases ago, D supports importing an entire package using the <code>package.d</code> module. This file is used as a fallback by the compiler if the imported module turns out to be a directory. Example: | ||
+ | |||
+ | <code>source/example/package.d</code> | ||
+ | <source lang="D"> | ||
+ | module example; | ||
+ | |||
+ | public import example.types; | ||
+ | public import example.api; | ||
+ | public import example.helpers; | ||
+ | </source> | ||
+ | |||
+ | <code>source/app.d</code> | ||
+ | <source lang="D"> | ||
+ | import example; // imports example/package.d | ||
+ | |||
+ | void main() { | ||
+ | // ... | ||
+ | } | ||
+ | </source> | ||
[[Category:CommonIdiom]] | [[Category:CommonIdiom]] |
Latest revision as of 23:37, 18 January 2018
In Java, if you want to import all of the modules in a package, you'd do this:
import javax.swing.*;
Since D doesn't support "*" imports (and it's unlikely that D would get such a shortcut), this capability could be added to a package by adding a module that imports the other modules. So what should such a module be called? There have been a few suggestions.
These are common conventions:
import mylibrary.mypackage.all;
import mylibrary.mypackage._;
package.d
Since several releases ago, D supports importing an entire package using the package.d
module. This file is used as a fallback by the compiler if the imported module turns out to be a directory. Example:
source/example/package.d
module example;
public import example.types;
public import example.api;
public import example.helpers;
source/app.d
import example; // imports example/package.d
void main() {
// ...
}