-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
spec: potential ambiguity in package initialization #70571
Comments
Related Issues
Related Code Changes Related Documentation
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.) |
As per the spec, the |
@rittneje The 3. rule specifies that the initialization expression must contain a reference to a function. The 1. rule defines such a reference as only an identifier. Similarly, the 2. rule defines reference to method. So, even though the 3. rule does not explicitly state anything about interfaces, we can infer from the 2. rule that in an expression |
@TutiFrouti It is not true that the initialization expression must contain a reference to a function. For example, In your case, the whole initialization expression is |
@rittneje I didn't say that the initialization expression must contain a reference to a function. I said that if it contains, according to the spec, that must be an identifier in order to establish the dependency. Spec (I bolded myself):
|
Omitting the parts not relevant to this conversation, the spec says:
For an anonymous func, the function's body is considered part of the expression, as per the spec. So it is by this spec point that we can conclude that |
see above. |
What is the URL of the page with the issue?
https://go.dev/ref/spec#Package_initialization
What is your user agent?
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
Screenshot
What did you do?
Does the variable
c
depends on the variablesa
andb
in the following program?In my understanding of the specification (package initialization), we have the following references:
t.m
wheret
is non-interface type (it doesn't matter if it's invoked) - 2. ruleAdditionally, spec says - 3. rule:
Back to my example. It is clear that "anonymous" function depends on the
a
andb
, but doesc
depend on thea
andb
? Spec says thatc
's initialization expression must contain reference to the function that depends on thea
andb
and my understanding is that this reference to the function (according to the 1. rule) is only by identifier, but not by "anonymous" function / function literal. Therefore, the reference to the function is defined by the 1st rule (same as the reference to the method is defined by the 2. rule).For me, this case is unspecified (same as with methods on interfaces).
However, when I print
c
it gives30
(it's okay to me, since it is unspecified; it can also be0
,10
,20
). Also, my colleagues and AI tools say that the dependency also holds in this case...Who is right, what is true?
What did you see happen?
I see that the result of the variable
c
is30
, but I'm not sure if this is "random" (since it is not specified) or the spec guarantees it even for the anonymous functions / function literals.What did you expect to see?
Resolving potential ambiguity. If I'm right, it would be great to potentially add an example with a function literal (as was done for interfaces and methods).
The text was updated successfully, but these errors were encountered: