Import proposals (Discuss)
Regan Heath
regan at netwin.co.nz
Tue Jul 11 17:25:18 PDT 2006
On Wed, 12 Jul 2006 02:20:06 +0300, Jari-Matti Mäkelä
<jmjmak at utu.fi.invalid> wrote:
> 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?
Ahh, I've done it again. That'll teach me for not at least coding my
little example up. I was assuming the import was private.. again.. (which
I think they should be, by default)
>> 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"); } }
> ?
No.. I was mistakenly assuming I could put _anything_ into a template,
including calls to writefln into 'main' .. turns out you can only put
stuff you could declare at the global scope in there. variables,
functions, etc. not calls to function. My bad.
>> 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?
Example?
Regan
More information about the Digitalmars-d
mailing list