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
Labels
Milestone

Comments

@xrfang
Copy link

@xrfang 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
@gopherbot gopherbot added the Proposal label Jul 11, 2019
@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Jul 11, 2019

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

This comment has been minimized.

Copy link
Author

@xrfang 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

This comment has been minimized.

Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Jul 12, 2019

This currently valid Go program would stop compiling.

package p

import "encoding/json"

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

This comment has been minimized.

Copy link
Contributor

@rsc rsc commented Jul 16, 2019

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

@rsc rsc closed this 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.