Import proposals (Discuss)

Jari-Matti Mäkelä jmjmak at utu.fi.invalid
Tue Jul 11 16:20:06 PDT 2006


Regan Heath wrote:
> On Tue, 11 Jul 2006 23:46:03 +0300, Jari-Matti Mäkelä
> <jmjmak at utu.fi.invalid> wrote:
>> Regan Heath wrote:
>>> On Mon, 10 Jul 2006 05:37:41 +0000 (UTC), Tyro
>>> <Tyro_member at pathlink.com> wrote:
>>>> In article <optcgh6iaa23k2f5 at nrage>, Regan Heath says...
>>>>>
>>>>> --[a.d]--
>>>>> import std.stdio;
>>>>> template foo { writefln("Hello World"); }
>>>>>
>>>>> --[b.d]--
>>>>> import std.stdio as bar;
>>>>> import a;
>>>>>
>>>>> void main() {
>>>>>   mixin foo;
>>>>> }
>>>>>
>>>>> I accidently called the template and import named scope the same
>>>>> thing.
>>>>>
>>>>> Regan
>>>>
>>>> In this case I assume that you are concerned with conflicts that may be
>>>> generated between both imports of std.stdio in [a.d] and [b.d].
>>>
>>> Nope. In a.d you can call "writefln" but in b.d you must call
>>> "bar.writefln", plain old writefln will fail, right?
>>> So, what does this mean for mixins?
>>
>> Are you referring to the existing functionality (sans implementation
>> bugs) or some specific proposal here.
> 
> The proposal to import a module into a namespace. In this case
> "std.stdio" into "bar", meaning "writefln" does not exist but
> "bar.writefln" does.

But if you import both:
 import std.stdio as bar;
 import std.stdio; (it's a public import, isn't it)

which (AFAIK) is exactly the same as

module a:
 import std.stdio;
module b:
 import a;
 import std.stdio as bar;

Doesn't that mean that you can use both:
[1]  writefln(...);
and
[2]  bar.writefln(...);
?

Which one of the proposals makes it illegal to call [1] in module b?

> All I was really asking was...
> 
> If module a.d imports "std.stdio" into the current namespace, then calls
> "writefln" in a template which is then mixed into another source file
> b.d, which imports "std.stdio" into a named namespace "bar", can that
> template call "writefln"?
> 
> I suspect the answer would have to be "no".

Um, I don't think so. The only thing that could make it illegal is that
 template foo { writefln("Hello World"); }
isn't valid D. Maybe it should be
 template foo { void hello() { writefln("Hello World"); } }
?

> After all the mixin is mixed
> into the scope of b.d, it doesn't exist in the scope of a.d where
> "writefln" is valid.

Correct. From http://www.digitalmars.com/d/mixin.html: "Unlike a
template instantiation, a template mixin's body is evaluated within the
scope where the mixin appears, not where the template declaration is
defined. It is analogous to cutting and pasting the body of the template
into the location of the mixin."

> All I was really trying to do was raise this as an issue which would
> occur (and need a solution) if we had import into namespace 'x'.

I think a more interesting use case would be to use real templates
(they're instantiated in module a) instead of mixins and module a that
is using a import statement that only imports some of the std.stdio
members. That might even cause some trouble?

-- 
Jari-Matti



More information about the Digitalmars-d mailing list