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

Bill Baxter wbaxter at gmail.com
Fri Nov 13 11:35:42 PST 2009


On Fri, Nov 13, 2009 at 11:12 AM, Clay Smith <clay.smith.r at gmail.com> wrote:
> Bill Baxter wrote:
>>
>> On Fri, Nov 13, 2009 at 10:23 AM, Clay Smith <clay.smith.r at gmail.com>
>> wrote:
>>>
>>> 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,
>>
>>
>> It's not that complicated.  Maybe try reading it again.  And forget
>> about the example of why you might want to import something without
>> using it, just trust that there are legit cases where you might want
>> to.
>
> ok
>
>>
>>>> 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
>>
>> Actually, I agree that the compiler should not error unused *public*
>> imports.  That really would not make any sense.
>
> True
>
>
>>
>>> Plus copy/paste imports from other modules, without compile error :o
>>
>> Why do you want to paste in a bunch of unused imports?   Wouldn't you
>> like to know which ones you use and which you don't, and trim the fat?
>>
>> --bb
>
> If I want to save time, and I'm developing a module similar to an existing
> one, copying/pasting imports is 90% accurate and saves me time, which is
> invaluable.

But if the compiler told you which ones you weren't using it wouldn't
take you much extra time to eliminate them.

What really sucks up time is having to remove imports one-by-one and
recompiling after each to try to figure out which imports you need and
which ones you don't.

--bb



More information about the Digitalmars-d mailing list