Skip to content
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

proposal: encoding/json: fluent interface for SetIndent/SetEscapeHTML #33051

Closed
xrfang opened this issue Jul 11, 2019 · 4 comments
Closed

proposal: encoding/json: fluent interface for SetIndent/SetEscapeHTML #33051

xrfang opened this issue Jul 11, 2019 · 4 comments

Comments

@xrfang
Copy link

xrfang commented Jul 11, 2019

In the stdlib, it's defined as:

func (enc *Encoder) SetIndent(prefix, indent string) {
    enc.indentPrefix = prefix
        enc.indentValue = indent
}

I suggest to make it fluent by:

func (enc *Encoder) SetIndent(prefix, indent string) *Encoder {
        enc.indentPrefix = prefix
        enc.indentValue = indent
        return enc
}

So that it is more convenient to do this:

json.NewEncoder(os.Stdout).SetIndent("", "    ").Encode(data)
@xrfang xrfang changed the title proposal fluent interface for json SetIndent/SetEscapeHTML proposal: fluent interface for json SetIndent/SetEscapeHTML Jul 11, 2019
@gopherbot gopherbot added this to the Proposal milestone Jul 11, 2019
@ianlancetaylor
Copy link
Contributor

Changing the method signature at this point would break the Go 1 compatibility guarantee (https://golang.org/doc/go1compat).

As a pure style point, Go code tends to use fluent interfaces less than in some other languages, in part because fluent interfaces don't work in Go for functions/methods that need to return errors.

@xrfang
Copy link
Author

xrfang commented Jul 12, 2019

OK, I am curious why this would break compatiblity?

If a function returns 1 variable, you now return 2, or none, that's a break of compatibility, but if the function originally returns nothing, now you return one (or any more) that will not affect any existing code?

@ianlancetaylor
Copy link
Contributor

This currently valid Go program would stop compiling.

package p

import "encoding/json"

var F func(*json.Encoder, string, string) = (*json.Encoder).SetIndent

@rsc
Copy link
Contributor

rsc commented Jul 16, 2019

This breaks compatibility, and it's not worth adding a second API.

@rsc rsc closed this as completed Jul 16, 2019
@rsc rsc changed the title proposal: fluent interface for json SetIndent/SetEscapeHTML proposal: encoding/json: fluent interface for SetIndent/SetEscapeHTML Jul 16, 2019
@golang golang locked and limited conversation to collaborators Jul 15, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants