-
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
fmt: add FormatString(State) string #51668
Comments
To clarify, I don't think we should add the (In retrospect I think it was a mistake to make |
Technically we can't add a method to the |
CC @robpike |
What would that let us do, though? Where I am imagining this would be most useful is in third party code that is inside of a func (b myType) Format(w fmt.State, verb rune) {
// here
} The general shape of that function, I would imagine, is something like the other proposal:
where ??? is, in theory, the recovered passed in format string. If edit: ahhh... if we added String() you could do |
@ianlancetaylor presumably it'd look like:
(that would mask any myType.String method though, and |
That said, I believe #51195 (comment) is cleaner.
|
I'm not sure we should be adding invisible methods here. The solution proposed in #51195 is completely visible - it doesn't need people to know things that aren't in the type system. A new method you type-assert would be different. |
This proposal has been added to the active column of the proposals project |
One possibility is to have a top-level function from fmt.State to string, like
|
Change https://go.dev/cl/400875 mentions this issue: |
@knz implemented https://github.com/knz/go-fmtfwd with some difficulty to work around this very issue. |
@robpike has posted a CL at https://go-review.googlesource.com/c/go/+/400875. The API is almost what I wrote earlier, adding 'verb rune' to the argument list:
This provides access to what was requested, just without a new State method. Does anyone object to this API? |
Actually it's not quite the same. The actual change is
as State does not contain the verb. The verb is always at the end, so the caller could add it, but doing it here can remove an allocation. |
Would it be possible to preserve a slice that refers to the portion in the original format string in the state, and return that instead of constructing a new string?
…--
Verstuurd vanaf mijn Android apparaat met K-9 Mail. Excuseer mijn beknoptheid.
|
No, because State is an interface. |
Updated comment to record 'verb rune' in API. |
Based on the discussion above, this proposal seems like a likely accept. |
No change in consensus, so accepted. 🎉 |
Sometimes when implementing a Formatter it's helpful to use the fmt package without invoking the formatter. This new function, FormatString, makes that easier in some cases by recreating the original formatting directive (such as "%3.2f") that caused Formatter.Format to be called. The original Formatter interface is probably not what we would design today, but we're stuck with it. FormatString, although it takes a State as an argument, compensates by making Formatter a little more flexible. The State does not include the verb so (unlike in the issue), we must provide it explicitly in the call to FormatString. Doing it there minimizes allocations by returning the complete format string. Fixes golang#51668 Updates golang#51195 Change-Id: Ie31c8256515864b2f460df45fbd231286b8b7a28 Reviewed-on: https://go-review.googlesource.com/c/go/+/400875 Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org> Reviewed-by: Russ Cox <rsc@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Run-TryBot: Russ Cox <rsc@golang.org>
I'm currently working with a custom fmt.Formatter implementation. Similar to the request in #51195, I would like to implement some methods and then "fall back" to the default
fmt
implementation for unimplemented verbs.It's difficult to do this because State does not allow you to reconstruct the original format string, unless you enumerate all of the possible characters in a format string and call
Flag(char)
on each one. This is lengthy and error prone.I would like to formally propose what @bcmills suggested in #25150, which is to add a String() api to
fmt.State
.I doubt that there are many implementations of the API, which would limit the amount of breakage from adding a new method.
In the standard library, there is currently only one implementation of
fmt.State
- in thepp
struct.If someone can give me pointers on how to do a search across all of Github, I would be happy to check whether there are in-the-wild implementations of fmt.State. I'd also appreciate if someone could do the same inside of Google.
Thanks to Bryan Mills, Github user seebs and Eric Lagergren for initial suggestions and discussion.
The text was updated successfully, but these errors were encountered: