-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
flag: Value description is inaccurate about zero-values #24439
Comments
Why is it not fine? Because the nil check is redundant in practice? /cc @robpike |
And because it panics on the *x.ptr dereferencing. |
If there's anything to do here, and I'm not sure there is, it would be to adjust the example to be more careful:
|
I would be happy to be proven wrong, but I think it deserves a little more justification than that.
Here's my proposed fix:
This is what actually happens in the code. |
In any event it seems like the flag package only ever uses Values that clients pass in. |
Assigning to @robpike to fix one way or another. |
@rsc I'm not sure what you mean. (Other Values not passed in by clients include the flag package's own private types which are used for all non-custom flags, but they work fine.) |
See also #28667. |
But in the example we have:
Which would panic if called with a nil pointer receiver.
Actually that code should be fine because String is never called with a nil pointer receiver (flag.isZeroValue uses reflect.New if the receiver is a pointer), but it is still contradictory.
In fact, from the current phrasing you could assume that doing something like this is fine, which is not:
Moreover from the flag.Value description it is not outright clear why the caller has to prepare to handle a value that he never produced.
Yes, the flag.PrintDefaults description answers that, but maybe it would be helpful to mention in the flag.Value description too, that the zero-value string representation is just used to turn off displaying the default (either because it is an unused value meaning off, or because it is an obvious default).
For instance if someone writes a flag.Value implementation whose zero-value (not the nil pointer) represents an inconsistent state (like I did), he would have to figure out what his String method is supposed to return in such a case and why it matters.
The text was updated successfully, but these errors were encountered: