Importing packages

Mike Parker aldacron71 at yahoo.com
Sat Oct 28 10:40:49 PDT 2006


Mart Roosmaa wrote:
> Hello everybody,
> 
> I am quite new to D and when learning how the packages and modules
> worked, it seemed strange to me that if one wanted to import whole
> packages one would have to create a module (for example a module called
> "All") and do public imports of every other module in the package.
> 
> While this approach works, it's quite painful to keep that module up to
> date, as people tend to forget to update the central module doing the
> public imports.
> 
> Now, I propose to extend the import declaration so that if only a
> package is given, it imports all modules in that package.
> For example "import std.c;" would import std.c.fenv, std.c.math,
> std.c.process, std.c.stdargs, etc modules.
> 
> This approach would keep the code a bit easier to maintain as one
> wouldn't have to update the public imports module all the time.
> 
> What do you think? Could it be implemented in DMD?

Even though Java allows this, it is generally regarded as poor practice. 
I have often seen coding standards that forbid it, requiring each class 
used to be imported explicitly. Explicit import of each class greatly 
reduces the chance of naming collisions, but also serves as a sort of 
documentation on which classes from a package are used in any given 
source file. Perhaps the most well-known examples of this are the Apache 
projects. If you look through the source to any Apache project, you 
won't find the use of .* imports.

If this were to be implemented in D, I would prefer to see the java 
syntax of mypackage.*, or something similar. Otherwise, it isn't 
immediately obvious by reading the code if an entire package is being 
imported or not. Consider this:

import foo.bar.blue;

Under your proposal, is blue a module or a package? It is assumed that 
users of a library would know the difference, since they are using the 
library anyway. But when it comes to code reviews, library examples, or 
any other situation where people not intimate with the package structure 
need to see the code, it adds to the learning curve and could cause 
confusion. Using the Java syntax, import foo.bar.*, makes it clear that 
all modules in bar are being imported.

I wouldn't use it myself, but I would only object to such a feature if 
implemented as you propose. Having an explicit syntax for package 
import, such as .*, would be okay. Though I'm of the opinion that use of 
it would reduce code quality.



More information about the Digitalmars-d mailing list