Warn on unused imports?

Dominikus Dittes Scherkl dominikus.scherkl at continental-corporation.com
Tue Sep 25 14:15:32 UTC 2018


On Tuesday, 25 September 2018 at 13:03:30 UTC, FeepingCreature 
wrote:
> I'm playing with a branch of DMD that would warn on unused 
> imports:
>
> https://github.com/FeepingCreature/dmd/tree/feature/Issue-3507-warn-on-unused-imports
>
> Two problems have arisen.
>
> First:
>
> import std.stdio;
>
> void foo(T)() { writeln("Hello World"); }
>
> foo.d: Warning: unused import
>
> To be fair, it's not *wrong*: if you remove the import, the 
> module itself compiles just fine. In any case, it's trivial to 
> instead move the import into the template.
>
> The real problem is this:
>
> import std.format;
>
> class TestException(T) : FormatException { }
>
> Now I can't move the import inside the template, because it's 
> needed at the point of instantiation, but not inside the 
> template scope *per se*.
>
> I could require the class to be written as
>
> template TestException(T) {
>   import std.format;
>   class TestException : FormatException { }
> }
>
> but that's kind of terrible.
>
> I've been working around this for now, with import std.format : 
> FormatException, but I'm not really happy with it.
>
> Any ideas?

Doesn't the "from" idiom work?
I'm not sure if it is allowed at the template declaration

template from(string moduleName)
{
   mixin("import from = " ~ moduleName ~ ";");
}

class TestException(T) from!"std.format".FormatException
  : FormatException
{
}


More information about the Digitalmars-d mailing list