-
Notifications
You must be signed in to change notification settings - Fork 17.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proposal: spec: file and package scope interact in odd ways #7429
Comments
Also, not allowing to shadow the imported package qualifier is somewhat inconsistently enforced only for TLDs: http://play.golang.org/p/77zRRbFxdI Relaxing the current rules to allow a TLD named x in a file which doesn't use x as an import qualifier even when some other file of the same package does would be IMHO an improvement. The existing rule can eg. make some trouble when moving code between packages, though probably not often. Perhaps: "... no identifier may be declared in both the file and package block from within the same source file"? |
Why this? Any bad to make top level identifiers declared in both file and package blocks? |
Aha, this is the same as the article mentioned above. I think this is a better approach.
Need changes in go/* packages? |
Playground link with demo: https://play.golang.org/p/ujhxUDdvktn |
The situation is exacerbated with dot-imports: A dot-import in one file will "pollute" the package-level namespace as no name that is dot-imported can be used in any other file of the same package. With this proposal, this pollution wouldn't matter because file scopes shadow the package scope. |
I think as long as you have the ability to track the file from which any non-imported top-level identifier came, you can keep their scopes disjoint. So for any imported identifier that is introduced, you do a recursive lookup. If the lookup succeeds, then you check that it's from the same file. If it is, then that's an error. Otherwise, the imported identifier shadows. Note that this allows you to keep top-level declarations inside package scope, and imports in file scope -- disjoint from each other. |
The text was updated successfully, but these errors were encountered: