You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The compiler is happy and you won't notice your mistake why nothing is being added (imagine a big loop around it where you are trying to add items to the slice) till you finally realize that the actual thing being to-be-added is missing [fortunately did not take more than 5 mins this time]
If the compiler is sneaky it could just optimize the above away if we are lucky, but it is very likely the implementer did not mean what is written.
Thus the big Q to avoid LateNightNightCoder problems:
That would indeed require elem to be processed outside the loop in append thus less good for the code, but would require at least one argument and make the compiler complain because the prototype requires at least one argument.
B) Prototype vararg annotation which indicates a requirement for minimal number of arguments
Another version would be where one could annotate a "minimum required varargs".
But I don't think that is very go to do pragmas or other such things.
Though, this can also be useful for other situations where one would like to have at least 20 params or something else silly (though pass them then as an array? :) )
C) Any other much better other option?
?
What would be a reasonable approach to avoid others to fall into this pitfall? :)
The text was updated successfully, but these errors were encountered:
@randall77 thanks for finding it, it is indeed a dupe of #30040 which has a lot more detail, and also simply shows ..... people run into this silently ;)
Suggested Label: #LateNightCoderDefence
TLDR: No compiler warning for "s = append(s)" which is a useless NOOP, would be good to have.
Hi,
A question, because this likely has been discussed before, and wanting to avoid other LateNightCoder's to run into this silly mistake.
Looking at the current branch (though this prototype likely never changed since the beginning):
We got in https://github.com/golang/go/blob/master/src/builtin/builtin.go#L134 :
func append(slice []Type, elems ...Type) []Type
which means that if you use:
`
var s []string
s = append(s)
`
The compiler is happy and you won't notice your mistake why nothing is being added (imagine a big loop around it where you are trying to add items to the slice) till you finally realize that the actual thing being to-be-added is missing [fortunately did not take more than 5 mins this time]
If the compiler is sneaky it could just optimize the above away if we are lucky, but it is very likely the implementer did not mean what is written.
Thus the big Q to avoid LateNightNightCoder problems:
Why not define append as:
A) Prototype with fixed arg
func append(slice []Type, elem Type, elems ...Type) []Type
That would indeed require elem to be processed outside the loop in append thus less good for the code, but would require at least one argument and make the compiler complain because the prototype requires at least one argument.
B) Prototype vararg annotation which indicates a requirement for minimal number of arguments
Another version would be where one could annotate a "minimum required varargs".
But I don't think that is very go to do pragmas or other such things.
Though, this can also be useful for other situations where one would like to have at least 20 params or something else silly (though pass them then as an array? :) )
C) Any other much better other option?
?
What would be a reasonable approach to avoid others to fall into this pitfall? :)
The text was updated successfully, but these errors were encountered: