Import concerns revisited

Georg Wrede georg.wrede at nospam.org
Thu Jul 13 00:22:41 PDT 2006


Walter Bright wrote:
> Derek Parnell wrote:
> 
>> On Thu, 13 Jul 2006 05:05:22 +1000, Walter Bright
>>
>>> It's not always true, but languages that are less wordy in their 
>>> example code tend to be picked up faster. People's first projects in 
>>> D aren't going to be large, complex, professional products. They'll 
>>> want to just try it out on something simple. That bar should be as 
>>> low as reasonable.
>>
>>
>> I understand your desire to not 'frighten' new comers away. My concern 
>> is that your "low as reasonable" seems to encourage poor programming 
>> practices. Can you provide an example where the 'import' and 
>> 'visibility' ideas being brainstormed at the moment would force 
>> example code to look scary? At worst, someone might have to add 
>> 'public { ... }' somewhere.

(I've been trying to catch up on this NG, but the number of unreads is 
growing faster than I can keep up, so I don't know what the Current 
Community Consensus on syntax exactly is right now, so I'm guessing the 
syntax here.)

> Not exactly what you're after, but consider the Java hello world program:
> 
>   class HelloWorldApp {
>     public static void main(String[] args) {
>         System.out.println("Hello World!");
>     }
>   }
> 
> There's a reason for everything there, but it's a little off-putting. 

Yes, from a teacher's point of view, the above is just depressing. On 
first day, you can't even explain the words because the needed concepts 
are pure greek to the audience anyway. I've actually been there a few times.

> Consider the D version:
> 
>   import std.stdio;
> 
>   void main() {
>      writefln("Hello World!");
>   }

D is a lot nicer. Now compare the above with:


     import std.stdio as io;

     void main() {
         io.writefln("Hello World!");
     }


Sure it's more to type! (Six letters, a dot and two spaces.)

 From an educational point of view, this is actually better than current 
D, because it now becomes perfectly clear /why/ we have the import line 
in the first place!!! It's needed for the writefln.

Additionally, I've seen students struggling with what comes from which 
library, why we need so many libraries, etc. All this would become much 
clearer if the students keep seeing "which words don't belong to the 
language itself and come from a library instead".

> I can go straighter towards what I want to do, without detouring through 
> all the other requirements Java puts on things that seem, with such a 
> simple program, to be beside the point.

I can, however, see this:

     import std.stdio as io;

     void main() {
         io.writefln("Hello World!");
         io.writefln("Hello World!");
         io.writefln("Hello World!");
         io.writefln("Hello World!");
         io.writefln("Hello World!");
         io.writefln("Hello World!");
         io.writefln("Hello World!");
         io.writefln("Hello World!");
         io.writefln("Hello World!");
         io.writefln("Hello World!");
         io.writefln("Hello World!");
     }

Now it's three letters more per line, so in a 100kloc program this means 
300kB extra on the hard disk, and some wear on your fingertips.

I admit we could have a means of using unqualified names, sure. But that 
should be the programmer's own choice!

     import std.stdio;                 // works like currently
     import std.stdio as foo;          // into foo namespace only
     import std.stdio.writefln;        // only writefln gets imported
     import std.stdio.writefln as wrt; // please implement this too

(Oh, and "as" is better than a space, if you ask me. No question.)



More information about the Digitalmars-d mailing list