Understanding the Use of Nested Import and Selective Import in D

user1234 user1234 at 12.de
Tue Jan 16 13:37:59 UTC 2024


On Tuesday, 16 January 2024 at 13:19:59 UTC, Orfeo wrote:
> I found myself a bit perplexed when it comes to the usage of 
> "nested imports" and selective imports. It seems that prominent 
> D programmers have varied opinions on the matter. I would love 
> to hear your insights and experiences on this topic.
>
> Here's a quick summary of what I've come across from three 
> influential D programmers:
>
> - Adam Ruppe: In his blog post titled [D's selective imports 
> have effects you may not 
> want](http://dpldocs.info/this-week-in-arsd/Blog.Posted_2023_11_06.html) have effects you may not want, Adam advises against the use of selective imports. He highlights potential unwanted side effects and suggests caution when employing them.
>
> - Atila Neves: At DConf 2023, Atila Neves recommended the use 
> of nested imports. He argues that nested imports can make 
> refactoring easier and help in assessing the dependencies a 
> function has.
>
> - Rober Schadek: Also at DConf 2023, Rober Schadek discouraged 
> the use of nested imports, taking a stance different from Atila 
> Neves.
>
> Now, the big question is: What's your preferred approach?

Another point is that the use of selective imports tends to be "a 
refactoring". You start with a global or local import. Once 
everything is fine you'll think that's it's a good idea to make 
the import selective, i.e "because I only use that in there".

Problem is, if someone else at some point work on your code:

1. he needs to add more to the selection
2. completion may not work anymore (will only show what's 
selected)

So it's a bit a thing of expert.

Given these arguments I think that global imports should not be 
selective, only local ones should.

Implementation detail. D frontend resolves identifiers using 
associative arrays (that's called symtabs in the compiler IIRC), 
hence the only complexity is the scope (plus the import decls 
found while going back to the module scope).


More information about the Digitalmars-d-learn mailing list