Comment #0 by peter.alexander.au — 2012-01-20T12:54:07Z
According to the language reference: "The order in which ImportDeclarations occur has no significance."
This is true for module scope imports in DMD (2.057 tested), but not for function scope imports.
---
import std.stdio;
void main() {
writeln("foo"); //ok
}
---
void main() {
writeln("foo"); //ok
}
import std.stdio;
---
void main() {
import std.stdio;
writeln("foo"); // ok
}
---
void main() {
writeln("foo"); // error, writeln undefined
import std.stdio;
}
---
Comment #1 by timon.gehr — 2012-01-20T13:55:42Z
I think what is meant in the reference is that the relative order of multiple ImportDeclarations is insignificant.
Comment #2 by peter.alexander.au — 2012-01-20T16:51:52Z
(In reply to comment #1)
> I think what is meant in the reference is that the relative order of multiple
> ImportDeclarations is insignificant.
True, it could be interpreted that way.
My understanding with D (in general) is that the ordering of all declarations is supposed to be inconsequential.
If this isn't the case and the code is in error by design then it should be clarified, especially since it is inconsistent with import declarations at module scope.
Comment #3 by issues.dlang — 2012-01-20T17:21:42Z
Yeah. I don't think that this is a bug, but then again, I wouldn't have been surprised if it didn't work to put imports at module scope after the line where you use what you're importing. It would just be so bizarre to put the imports anywhere but at the top that I've never thought about it.
But consider that the order of declarations of module variables doesn't normally matter (it can with static if), but it matters a great deal at local scope. So, for imports to work the same makes sense.
I'm not quite sure how the language spec would be updated though. I don't know how you could have interpreted it to mean that you could put them after the line where you used what you're importing within a function. I would have thought that it was crystal clear as it is.
Comment #4 by timon.gehr — 2012-01-20T17:54:22Z
Also see issue 7329.
Comment #5 by peter.alexander.au — 2012-01-21T03:51:22Z
(In reply to comment #3)
> I'm not quite sure how the language spec would be updated though. I don't know
> how you could have interpreted it to mean that you could put them after the
> line where you used what you're importing within a function. I would have
> thought that it was crystal clear as it is.
Well, it can't be crystal clear because you were unaware that you could put imports at module scope after the use of the imported symbols :-)
One way would be to add a statement to the effect of:
"At module scope, import declarations may appear after the use of imported symbols. However, in local scopes, import declarations must appear before the use of any symbols introduced by the import."