Nested public imports - bug or feature?

Timon Gehr via Digitalmars-d digitalmars-d at puremagic.com
Thu Aug 13 06:56:21 PDT 2015


On 08/13/2015 03:42 PM, Jonathan M Davis wrote:
> On Thursday, 13 August 2015 at 13:12:44 UTC, Dicebot wrote:
>> Right now this works:
>>
>> ``D
>> struct Std
>> {
>>   public import std.stdio;
>> }
>>
>> void main()
>> {
>>   Std.writeln("Nice!");
>> }
>> ```
>>
>> I want to use it as an import hygiene idiom but not entirely sure if
>> this behavior can be relied upon (or it is just a side effect of
>> imports being implemented as aliases currently).
>
> Well, that's pretty much why splitting up a module and putting public
> imports in its package.d file doesn't break any of the cases where
> someone types out the full import path when referring to something from
> that module/package. But I doubt that anyone considered that that would
> have this effect when you have a scoped import. In fact, to be honest,
> it never occurred to me that it would be legal to have a scoped, public
> import. I think that you just hit a weird result of how allowing imports
> to be put everywhere ended up working.
>
> I confess that I don't particularly like that this is legal (and I think
> that public imports tend to get a bit hinky because of the fact that
> they create aliases), and I'm not quite sure how you could use it form
> "import hygiene,"

It has nothing to do with the import being public. This works:

---
struct Std{
     import std.stdio;
}
void main(){
     Std.writeln("Nice!");
}
---

(It also works if main and Std are defined in different modules.)

> but I also don't see how this could work any other way
> if scoped, public imports are allowed.
>...

Easy. Just treat aggregate scopes and module scopes differently. (They 
are treated differently even now: all imports are 'public' in aggregate 
scopes, but not at module scope.) I think this shouldn't be done though.

In any case, I guess we agree that this idiom should work for public 
imports, but not for non-public ones (so the current behaviour with 
non-public imports is accepts-invalid, but Dicebot's code should be fine)?


More information about the Digitalmars-d mailing list