www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - "Global" imports vs scoped imports

reply "Jeremy DeHaan" <dehaan.jeremiah gmail.com> writes:
I am working on a library, and I put all imports at the top of my 
source files under the module declaration. I was starting to 
think that it might be a good idea to start scoping some imports 
to reduce the number that might be used by a giving project, but 
how beneficial is this? Are there any kind of general rules for 
when to use module level imports vs scoped imports?

Thanks!
      Jeremy
Feb 09 2014
next sibling parent reply "Dicebot" <public dicebot.lv> writes:
On Sunday, 9 February 2014 at 19:12:21 UTC, Jeremy DeHaan wrote:
 I am working on a library, and I put all imports at the top of 
 my source files under the module declaration. I was starting to 
 think that it might be a good idea to start scoping some 
 imports to reduce the number that might be used by a giving 
 project, but how beneficial is this? Are there any kind of 
 general rules for when to use module level imports vs scoped 
 imports?

 Thanks!
      Jeremy
Use scoped imports as much as possible. Those are completely superior to global ones. Pretty much only reason to have something as global import is if it is used in template constraints or vast majority of functions/methods of the module. Usage of selective imports is also encouraged. Main benefits are lazy evaluation and simplified maintenance - unused import become obvious, as well as symbol origins in function bodies.
Feb 09 2014
next sibling parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Sunday, February 09, 2014 19:18:05 Dicebot wrote:
 Usage of selective imports is also encouraged.
Not so much. At least, not right now. They don't work right and tend to cause symbol conflicts: https://d.puremagic.com/issues/show_bug.cgi?id=314 https://d.puremagic.com/issues/show_bug.cgi?id=8667 Now, once they're fixed, I could see an argument for using them, but until then, I'd advise staying away from them - at least in library code where the modules that you're writing are likely to be imported by other modules; it won't matter so much if it's the module with main in it and nothing is importing it. Certainly, using them in something like Phobos is generally a bad idea at this point. Personally, I'm not sure that I'd use them even once they're fixed unless I really only need one or two symbols out of the module, as it gets tedious otherwise, but I know that some people do really like the idea of making it very explicit which symbols are being used from a module being imported. - Jonathan M Davis
Feb 09 2014
next sibling parent "Jeremy DeHaan" <dehaan.jeremiah gmail.com> writes:
On Monday, 10 February 2014 at 00:30:14 UTC, Jonathan M Davis 
wrote:
 On Sunday, February 09, 2014 19:18:05 Dicebot wrote:
 Usage of selective imports is also encouraged.
Not so much. At least, not right now. They don't work right and tend to cause symbol conflicts: https://d.puremagic.com/issues/show_bug.cgi?id=314 https://d.puremagic.com/issues/show_bug.cgi?id=8667 Now, once they're fixed, I could see an argument for using them, but until then, I'd advise staying away from them - at least in library code where the modules that you're writing are likely to be imported by other modules; it won't matter so much if it's the module with main in it and nothing is importing it. Certainly, using them in something like Phobos is generally a bad idea at this point. Personally, I'm not sure that I'd use them even once they're fixed unless I really only need one or two symbols out of the module, as it gets tedious otherwise, but I know that some people do really like the idea of making it very explicit which symbols are being used from a module being imported. - Jonathan M Davis
Awesome. Thanks for the information. I've found a few places I think it makes sense, but I was a little worried about doing it everywhere. Code bloat city.
Feb 09 2014
prev sibling parent reply "Dicebot" <public dicebot.lv> writes:
On Monday, 10 February 2014 at 00:30:14 UTC, Jonathan M Davis 
wrote:
 On Sunday, February 09, 2014 19:18:05 Dicebot wrote:
 Usage of selective imports is also encouraged.
Not so much. At least, not right now. They don't work right and tend to cause symbol conflicts: https://d.puremagic.com/issues/show_bug.cgi?id=314 https://d.puremagic.com/issues/show_bug.cgi?id=8667
I was speaking about _both_ scoped and selective imports. 314 is not an issue for those at all and 8667 is relatively hard to trigger. Global selective imports are bad idea for sure.
Feb 10 2014
parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Monday, February 10, 2014 12:21:20 Dicebot wrote:
 I was speaking about _both_ scoped and selective imports.
Well, personally, I'd _never_ use a scoped selective import. At that point, you have the whole function body in front of you, so it should be obvious what was being used from the module being imported, and the selective import doesn't add much besides extra keystrokes. But then again, I've never really been a fan of selective imports in the first place. With selective imports, you're essentially importing individual symbols rather than modules, which makes dealing with them that much more tedious. But to each their own. - Jonathan M Davis
Feb 10 2014
prev sibling parent reply "Gary Willoughby" <dev nomad.so> writes:
On Sunday, 9 February 2014 at 19:18:06 UTC, Dicebot wrote:
 Usage of selective imports is also encouraged.
With the caveat that it only affects namespace hygiene. http://forum.dlang.org/thread/dstkqarbuhrnzbjjffiy forum.dlang.org
Feb 10 2014
parent reply "Dicebot" <public dicebot.lv> writes:
On Monday, 10 February 2014 at 12:55:44 UTC, Gary Willoughby 
wrote:
 On Sunday, 9 February 2014 at 19:18:06 UTC, Dicebot wrote:
 Usage of selective imports is also encouraged.
With the caveat that it only affects namespace hygiene. http://forum.dlang.org/thread/dstkqarbuhrnzbjjffiy forum.dlang.org
As if hygiene is not important :P
Feb 10 2014
parent "Gary Willoughby" <dev nomad.so> writes:
On Monday, 10 February 2014 at 13:09:32 UTC, Dicebot wrote:
 On Monday, 10 February 2014 at 12:55:44 UTC, Gary Willoughby 
 wrote:
 On Sunday, 9 February 2014 at 19:18:06 UTC, Dicebot wrote:
 Usage of selective imports is also encouraged.
With the caveat that it only affects namespace hygiene. http://forum.dlang.org/thread/dstkqarbuhrnzbjjffiy forum.dlang.org
As if hygiene is not important :P
I agree it is, but don't assume (as i did) it affects compilation. :)
Feb 10 2014
prev sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Sunday, February 09, 2014 19:12:19 Jeremy DeHaan wrote:
 I am working on a library, and I put all imports at the top of my
 source files under the module declaration. I was starting to
 think that it might be a good idea to start scoping some imports
 to reduce the number that might be used by a giving project, but
 how beneficial is this? Are there any kind of general rules for
 when to use module level imports vs scoped imports?
The main place to use scoped imports in templated functions, because then the imports are only used if/when the template is instantiated. Outside of templated code, it's a matter of style and depends on how much in the module needs that import. For instance, if you're using std.range everywhere in a module, it's kind of silly to import it in every function in your module, as that's a lot of extra lines of code for little benefit. At that point, it's arguably better to just import it at the top. On the other hand, if you only using it within a handful of functions, by scoping the imports, it's clear what's using it, and if those functions all went away or were refactored such that they didn't need std.range anymore, then it would be clear that the imports could go away, whereas if the import is at the module level, it's not always obvious when you don't need it anymore. - Jonathan M Davis
Feb 09 2014