import concerns (was Re: Historical language survey)

Kirk McDonald kirklin.mcdonald at gmail.com
Sat Jul 8 13:43:56 PDT 2006


Walter Bright wrote:
> Walter Bright wrote:
> 
>>>> What can also be done is extend the import declaration to allow the 
>>>> .'s to continue so that specific symbols can be imported.
>>>
>>>
>>> Now that would be great. I believe selective-importing (as an option) 
>>> would be a boon in a number of ways ~ and would resolve this issue 
>>> quite elegantly.
>>
>>
>> I like this one better, too.
> 
> 
> There's another way - have a different kind of import declaration, say, 
> precede it with static:
> 
>     static import foo;
> 
> which will make the symbols in foo available, but only if they are 
> explicitly qualified. Then one could access bar in foo by either:
> 
>     foo.bar();
> 
> or:
> 
>     alias foo.bar bar;
>     bar();
> 
> but not:
> 
>     bar();    // error, undefined symbol
> 
> The advantage of this is it is a bit more flexible and more consistent 
> with the way the rest of D lookups work.

I'm glad you liked my "static import" idea, but I have an additional 
suggestion.

Say we have the following module:

[foo.d]
module foo;

int bar;
char[] baz;
// ... and other names...

// EOF

I propose the following:

import foo;
     Does what it does now.

static import foo;
     Requires names in foo to be fully-qualified. Also, this should be 
implicitly private, so that modules that in turn import this one do not 
get the name "foo" inserted into their own namespaces.

static import foo with bar, baz;
     The names bar and baz can be accessed directly, any other names 
must be fully-qualified. (This reuses the 'with' keyword.) This should 
be synonymous with:

     static import foo;
     alias foo.bar bar;
     alias foo.baz baz;

     And it is much less verbose.

static import foo with bar as boo, baz;
     foo.bar can be accessed as boo, foo.baz as baz, and any other names 
in foo must be fully qualified. This should be the same as:

     static import foo;
     alias foo.bar boo;
     alias foo.baz baz;

     And (again) it is much less verbose. This has the disadvantage of 
requiring a new keyword. The word "alias" can be used in place of "as" 
to prevent this, though I don't think it reads as well.

import foo with bar, baz;
     This would import foo using the current rules, and promote bar and 
baz to first-class names, shadowing names imported from other modules. 
Any other names in foo would remain second-class names. Analogously to 
above, this is the same as:

     import foo;
     alias foo.bar bar;
     alias foo.baz baz;

     The syntax "import foo with bar as boo;" should obviously also be 
allowed.

-- 
Kirk McDonald
Pyd: Wrapping Python with D
http://dsource.org/projects/pyd/wiki



More information about the Digitalmars-d mailing list