-
Notifications
You must be signed in to change notification settings - Fork 285
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
chore: add missing tests for var-declarations, fix any/interface{} type issue #1154
Conversation
|
||
func zeroValueExample() { | ||
var y int // No warning, zero value is implicit | ||
var z1 any = 0 // No warning, zero value for any is nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO, in cases of the form var <id> any = <value>
where value is not nil
the rule must warn with should omit type any from declaration of var <id>; it will be inferred from the right-hand side
because var <id> = <value>
is equivalent
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be clear, are you saying that var <id> any = <non_nil_value>
is the same as var <id> = <value>
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes
https://go.dev/play/p/TORLftc7jlR with any
https://go.dev/play/p/dYvM0OlUstA without any
both outputs are the same
a value's is 100 and it is of type int
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if an initial value is provided then declaring the variable of being type any
has no utility
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still, I think there is a difference. With any
it is a dynamic type and it can be changed to any value later. Consider this:
package main
func main() {
var a1 = 1
// a1 = "string" // fail to compile
var a2 any = 1
a2 = "string" // this will compile
_ = a1
_ = a2
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
declaring the variable of being type
any
has no utility
I believe there can be a reason to declare an any
type variable in case if you plan to have an arbitrary value for it later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ooops; you are right... I did not think in these kind of (ugly) cases
@chavacava I updated the tests. Are you fine with the current state? |
I don't understand the last commit that provide a strange import order |
It reverts what I committed by mistake. |
Fixes #1152