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: Go 2: permit a struct type to also be a function #25757

Closed
dans-stuff opened this issue Jun 6, 2018 · 2 comments
Closed

proposal: Go 2: permit a struct type to also be a function #25757

dans-stuff opened this issue Jun 6, 2018 · 2 comments
Labels
FrozenDueToAge LanguageChange Proposal v2 A language change or incompatible library change
Milestone

Comments

@dans-stuff
Copy link

dans-stuff commented Jun 6, 2018

I often find myself writing "config" structs that I pass to constructors, or complicated functions that I add as methods just for organization. There's some stuttering where I have to give the method a name, which ends up being completely useless as it's just "initializating" the struct.

It would be great if I could define a usual method that would be called if I try to "call" an instance of the struct, basically if I omit the name from a struct.Method() call, it would use this default method.

type GetSingleUser struct {
	IDOfUser int `mux:"id"`
	Response struct {
		Found   *ExistingUser `rest:"ok"`
		Missing bool          `rest:"missing"`
	} `rest:"response"`
}, func (u *GetSingleUser) (find func(*ExistingUser) error, log Logger) {
	existingUser := ExistingUser{ID: u.IDOfUser}
	if err := find(&existingUser); err != nil {
		u.Response.Missing = true
		log.F("didn't find user %d", u.IDOfUser)
		return
	}
	u.Response.Found = &existingUser
	log.F("found user %d", u.IDOfUser)
}

myUser := GetSingleUser{IDOfUser: 10}
myUser(bindings.Find, logger.Stdout)
fmt.Printf("found: %t", !myUser.Response.Missing)

Or for creating anything complicated that has some initialization code, instead of New.

dialer := rpc.Dial{Network: "tcp", Address: "example.com", Path: "/rpc"}
go dialer()

I don't really need too many examples, as they effectively replace "Keyword Arguments" in Python and other languages.

I understand the syntax might not be great, definitely open to suggestions, but some nameless inline method would be a real nice-to-have for a lot of cases, as well as introduce up a small option for API/library design.

@dans-stuff dans-stuff changed the title Allow a struct to define itself as a function. proposal: Allow a struct to define itself a function. Jun 6, 2018
@gopherbot gopherbot added this to the Proposal milestone Jun 6, 2018
@ianlancetaylor ianlancetaylor changed the title proposal: Allow a struct to define itself a function. proposal: Go 2: permit a struct type to also be a function Jun 6, 2018
@ianlancetaylor ianlancetaylor added LanguageChange v2 A language change or incompatible library change labels Jun 6, 2018
@OneOfOne
Copy link
Contributor

OneOfOne commented Jun 8, 2018

How is that better than https://play.golang.org/p/4546y7dliLx?

type S struct { ... }
func (s *S) DoStuff() { ... }
....
s := S{....}
s.DoStuff()
// or
(&S{...}).DoStuff()

@ianlancetaylor
Copy link
Contributor

This is basically the facility to define a method with no name, which can be invoked by simply calling the object. You could get almost the same effect by defining a new method M, except that you would have to write .M in the call. We don't need syntactic sugar here. Thanks for the suggestion.

@golang golang locked and limited conversation to collaborators May 30, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge LanguageChange Proposal v2 A language change or incompatible library change
Projects
None yet
Development

No branches or pull requests

5 participants