How about Go's... error on unused imports?

Clay Smith clay.smith.r at gmail.com
Fri Nov 13 10:23:44 PST 2009


Clay Smith wrote:
> Bill Baxter wrote:
>> On Fri, Nov 13, 2009 at 9:51 AM, Clay Smith <clay.smith.r at gmail.com> 
>> wrote:
>>> Leandro Lucarella wrote:
>>>> I'm sorry to bring up another "How about Go's ..." topic, but I 
>>>> think this
>>>> feature is very in sync with D and it should be really easy to 
>>>> implement.
>>>>
>>>> Go issues an error if you have an import that's never used, to avoid
>>>> unnecessary dependencies.
>>>>
>>>> Do you see any reasons not to do that? I think it happens very often to
>>>> stop using some import and never notice it.
>>>>
>>> No, this is a job to be left to the IDE, not compiler.
>>
>> Well, just a small argument for the contrary, but you may be importing
>> some module precisely for the purpose of getting its module
>> constructors to run.  For instance the module constructor may have a
>> call like RegisterComponent(thisModule) in it.  Think of a set of
>> image format loaders that register themselves with the main
>> ImageLoader module.  Then you call ImageLoader.load(filename).
>> Nowhere in your code do you care if it is a PNG or not.  You only need
>> to use the ImageLoader's API.
>>
>> Also there's public imports.  And all.d type modules with nothing but
>> pubilic imports.
>>
>> There's no way an IDE can know about those intents.  So that argues
>> that there should be a way in the language to indicate "i know it
>> doesn't look like I'm using this, but I am".   And if that's in the
>> language then the compiler might as well do the checking, too.
>>
>> Ok, not a super strong argument... unless D changed to compile things
>> the way Go does, rolling in all deps.
>>
>> How about that one from Go?  Including all deps in an obj.  Could D
>> use the same trick to speed up compilation further?
>>
>> --bb
> 
> I'm not really understanding what you are saying, but I see no need for 
> the compiler to error on unused import. If anything, the compiler can 
> just ignore it, or have a flag to find unused imports. I would like my 
> all.d files to not give errors :o

Plus copy/paste imports from other modules, without compile error :o



More information about the Digitalmars-d mailing list