D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 12314 - Allow Duplicate Renamed Imports
Summary: Allow Duplicate Renamed Imports
Status: RESOLVED WONTFIX
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-03-07 12:19 UTC by Jeroen Bollen
Modified: 2022-08-22 12:08 UTC (History)
5 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Jeroen Bollen 2014-03-07 12:19:54 UTC
It should be allowed for renaming the same import twice, with the same name. 

    // This should be possible
    private import gtk = gtk.Application;
    private import gtk = gtk.Window;
Comment 1 bearophile_hugs 2014-03-07 13:06:25 UTC
(In reply to comment #0)
> It should be allowed for renaming the same import twice, with the same name. 
> 
>     // This should be possible
>     private import gtk = gtk.Application;
>     private import gtk = gtk.Window;

Please list what are the advantages an disadvantages of this proposal. (At first sight I don't like it, but perhaps I am wrong).
Comment 2 Jeroen Bollen 2014-03-07 13:10:07 UTC
(In reply to comment #1)
> Please list what are the advantages an disadvantages of this proposal. (At
> first sight I don't like it, but perhaps I am wrong).

The main advantage is that when you have libraries like the GtkD which I used in the first post, you can put them all in one namespace. 

This way you don't get the redundancy of specifying the exact module name for every call to a function, but you still keep your code clean and maintainable by specifying what library a certain class/struct/object belongs to.
Comment 3 bearophile_hugs 2014-03-07 13:24:32 UTC
(In reply to comment #2)
> (In reply to comment #1)
> > Please list what are the advantages an disadvantages of this proposal. (At
> > first sight I don't like it, but perhaps I am wrong).
> 
> The main advantage is that when you have libraries like the GtkD which I used
> in the first post, you can put them all in one namespace. 
> 
> This way you don't get the redundancy of specifying the exact module name for
> every call to a function, but you still keep your code clean and maintainable
> by specifying what library a certain class/struct/object belongs to.

What are the disadvantages?
Comment 4 Jeroen Bollen 2014-03-07 13:26:21 UTC
(In reply to comment #3)
> (In reply to comment #2)
> > The main advantage is that when you have libraries like the GtkD which I used
> > in the first post, you can put them all in one namespace. 
> > 
> > This way you don't get the redundancy of specifying the exact module name for
> > every call to a function, but you still keep your code clean and maintainable
> > by specifying what library a certain class/struct/object belongs to.
> 
> What are the disadvantages?

I can't really think of any. Obviously it can cause conflicts if you have two matching symbols and you put them in the same module/namespace; but conflicts are already possible as-is, and should be avoided.
Comment 5 Vladimir Panteleev 2014-03-07 13:28:17 UTC
How is this different from a package.d file?
Comment 6 Jeroen Bollen 2014-03-07 13:33:17 UTC
(In reply to comment #5)
> How is this different from a package.d file?

You mean having a separate module publicly importing all the required modules? 

The difference would be that not a separate module would be needed for every different set of includes possible. Obviously you could make one file simply importing everything, but that'd just be an over-kill. When a module only uses 2 or 3 modules it's not worth importing every single one of them.
Comment 7 Nick Treleaven 2019-01-06 13:00:07 UTC
This could be implemented with a struct template:

alias gtk = MergeImports!"gtk.Application, gtk.Window";

The struct would expose the listed modules' members as aliased struct members.

I think this should be closed as WONTFIX, because it adds complexity for the user when analysing modules for a case which might not be common enough, and can be done as above.
Comment 8 RazvanN 2022-08-22 12:08:37 UTC
I don't really see any benefit in allowing this sort of behavior. If you have only 2-3 modules that you are importing you can either just put those imports top-level or just use them selectively inside the functions that require them. I think that D already provides plenty of options for such scenarios, therefore it is not needed to further complicate the language implementation to allow such a narrow use case.

If needed, the workaround presented by Nick is applicable.