| > It's forcing you to do the right thing. Mmm... No. It's just post-hoc justification of the design. > > Why does it need to include any other unused module? > Because without at the very least parse the entire dependency tree of the module (which could be huge) it cannot know if it's unused. Once again: no. Because it already knows it's unused and stops the compilation. So it already knows it's unused. > If you import it then it's not unused, and it's extremely common that that's a mistake. That's why. Once again: - the compiler has already parsed everything - the compiler already knows that the import is unused - the compiler stops the compilation at this point ... because otherwise it would have to include anyway even though at this point it already knows that the import is unused > How many times do you just leave "#include <sys/types.h>" because you don't know exactly why you imported it? There are other ways of dealing with this than halting the compilation entirely. |
Do you have any reason to think that? E.g. documented or discussions at the time?
> Once again: no. Because it already knows it's unused and stops the compilation.
Hmm… ok, I see what you may be saying now. Am I right in saying that you would prefer that Go would silently ignore any non-underscore imports that aren't referenced by name?
So if I import "fmt" and don't use it, then the compiler won't even try to open the associated pkg files?
And if I want the compiler to override that (because I'm importing for the side effects), then I'll have to do the underscore trick to force the import even though it's unused?
Is that what you mean?
That seems like it would be a gotcha. As a reader of the code (not the compiler) I would then not be able to see the list of imports and actually know which ones actually get imported. Like, dammit why doesn't sql support pgsql? Imported the pgsql driver, it's right there!
If you mean "no, not silently ignore. Give a warning", then that runs into the (eyerolling) "the Go compiler doesn't have warnings. It runs effectively -Werror. It's eyerolling because it's clearly not true, as they've simply outsourced those second class warnings to govet and other tools.
But anyway, Go's forcing of this makes it so that the reader actually knows what actually gets imported.
I hope you at least recognize that because of side effects (which is true in Python (code executes on import), C++ (global variable constructors), C (attribute constructor), and others, importing and not using does not create the same behavior as not importing at all?
I don't want the compiler to make that choice for me.
If the Go compiler second-guessed an import, it would be an outlier among languages. The three others I listed don't.
And Go is baking IWYU into the compiler. You may not like it, but it's not stupid.
> There are other ways of dealing with this than halting the compilation entirely.
Oh I agree. I find the "unused variable" compile error to be hugely frustrating during development. Having to work around with "a = a" or wrapping code in "if false {}" instead of commenting out is ridiculous.
But the import one I agree with.