Thoughts about modules

Kirk McDonald kirklin.mcdonald at gmail.com
Thu Jun 29 14:45:23 PDT 2006


Derek Parnell wrote:
> On Fri, 30 Jun 2006 07:09:47 +1000, Kirk McDonald  
> <kirklin.mcdonald at gmail.com> wrote:
> 
> 
>> Some additional features of Python. :-)
> 
> 
> But back to the question I asked ...
> 
> Why is FQN syntax demonstrable better than the current shortcut syntax?
> 
> Alternatively, why is ...
> 
>  import std.stdio;
>  writefln( ... );
> 
> a bad thing?
> 

Here is a more illustrative example:

[test1.d]
module test1;

const int foo = 10;
// EOF

[test2.d]
import test1;
import std.stdio;

void main() {
     writefln("%s", foo);
}
// EOF

The name "foo" comes from the module test1, of course. We know this 
because we are familiar with test1, and because it is a small module 
that we can easily examine the contents of. It is fairly obvious where 
"foo" comes from.

However, imagine that "test1" is actually the main import point of a 
large package. Say it in turn imports dozens of modules, and foo 
actually comes from one of them. Now say you're importing more than one 
huge package like this, so now foo might come from anywhere.

Now say that this is someone else's code, or even your own code from a 
few months after you wrote it. Goodness gracious, what does "foo" mean?

Now, you might fully-qualify the name to make this obvious, but this can 
easily get cumbersome if you are using foo more than once. It suddenly 
becomes useful to write an alias:

     alias test1.foo foo;

Now the person reading your source file can see this alias, right there 
in the same file, and they can know where this "foo" comes from. The 
Python syntax

 >>> from test1 import foo

is simply a shortcut for this alias. By making it mandatory, we can 
always know where any name comes from, whether they are being 
fully-qualified, or imported on their own.

Now, for something like writefln, which is a well-known name that 
everyone will recognize instantly, this is a bit of a burden. You'd have 
to type it a whole extra time at the top of the file! (Python manages to 
escape this by having the "print" keyword.) However, I feel this method 
scales better.

-Kirk McDonald



More information about the Digitalmars-d mailing list