Cool project idea: organise/remove imports

Atila Neves atila.neves at gmail.com
Mon Jul 15 11:34:02 UTC 2024


On Friday, 12 July 2024 at 09:37:46 UTC, Vladimir Panteleev wrote:
> On Thursday, 11 July 2024 at 10:28:35 UTC, Atila Neves wrote:
>> It'd be amazing if one could hit a key/key combination and 
>> have all imports pared down to the absolute minimum (and made 
>> local). If only I had the time...
>
> I made this a while ago. It's not quite what you describe - 
> instead of typing `import std;`, you don't type any imports at 
> all, and instead the tool uses compiler error messages + 
> `.json` output to add missing imports.
>
> https://forum.dlang.org/post/baayfburkivhwurpepav@forum.dlang.org
>
> Code is here: https://github.com/CyberShadow/AutoFix (now 
> includes an Emacs package).
>
> I'm not sure that local imports are best for all cases. They're 
> good for trivial things like pure parsing functions wanting to 
> import some utility module. Otherwise, having them at the top 
> of the module is a good way to document and understand the 
> relationships of the module with other parts of the program - 
> for instance, networking imports tell you right away that it 
> talks to other things on the network, filesystem imports tells 
> you it uses files, same with custom subsystems.

Nice!

I think local imports are always superior, because they help with 
refactoring. I've had DCD choke on where certain symbols come 
from as well and have lost count of how many times I've had to 
localise and make explicit every import in order to find out.

It's also easier to hide dependencies in module-level imports. A 
function with 20 imports is clearly doing something wrong but 
that doesn't stand out if all of them are at module level instead.


More information about the Digitalmars-d mailing list