You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
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
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
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.
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.Or for creating anything complicated that has some initialization code, instead of New.
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.
The text was updated successfully, but these errors were encountered: