-
Notifications
You must be signed in to change notification settings - Fork 17.5k
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: allow type-parameterized methods. #43390
Comments
CC @griesemer |
As you say, methods are syntactic sugar for functions, except that methods also satisfy interfaces. If we drop the possibility of satisfying interfaces, then it's not too obvious what we get from methods. |
Mostly it seems to be useful for ergonomic reasons. For example, the classic functional iterator patterns are very clunky with top-level functions, but they work a bit better with methods: // Top-level functions read backwards and visually separate logic in
// callbacks from calling the function, along with making it an
// absolute pain to modify the chain later, such as by inserting a
// map:
sum := iter.Reduce(
iter.Filter(
iter.Slice(someSlice),
func(v int) bool { return v > 0 },
),
0,
func(sum, cur int) int { return sum + cur },
)
// Methods chain weirdly because of semicolon rules, but otherwise
// read much more cleanly and are far more editible:
sum := iter.Slice(someSlice).
Filter(func(v int) bool { return v > 0 }).
Reduce(0, func(sum, cur int) int { return sum + cur }) Personally, I think that it's worth it even without interface satisfaction, but I won't be too annoyed if the initial version of generics doesn't support them. I think it's worth keeping the discussion open, though, even after generics (hopefully) get put in. I also think that generic types should get inference the same as functions have if at all possible for similar reasons. |
When using Go reflection to introspect the set of methods on a type with type-parameterized methods, what happens? |
If interface satisfaction isn't allowed with generic methods, at least at first, then I think that it's probably not too surprising if they also can't be seen at all with |
type Getter[T any] interface {
Get() T
}
type Zero struct {}
func (Zero) Get[T any]() ( _ T) {
return
} In this example, would be |
@tdakkota Excellent question. In so far as visibility is concerned (as in the issue described in the current proposal), I believe this could work BUT it's more complex, because after specialization with e.g.
At first glance, Secondly, the constraints on My opinion is that there might be a way, but it's complex enough that I would leave it out for now. Baby steps. |
Putting this on hold pending any decision on incorporating generics into the language. |
Could this be put on the table for 1.20? |
Thanks for raising this issue. Although this issue is older, there is a lot more discussion on #49085, so closing this issue in favor of that one. |
Proposal to allow type-parameterized method definition in go2go
Introduction
The relevant section of the current proposal can be found here.
The idea
It's simple: Disallow type-parameterized methods in interface bodies only.
To illustrate what I mean (excerpt from the proposal):
But do allow the line (also from the proposal):
The arguments
Symmetry
Go methods are syntactic sugar for functions. They're one and the same, receivers are first arguments. An example:
Making a distinction between functions and methods would break this symmetry.
Usefulness
An obvious use-case that comes to mind a Result type (a Functor in Haskell-speak):
Note how Map requires an additional type-parameter, U. Here's a (silly) way it could be used:
Other concepts that can be implemented in similar ways are e.g. Promises, Monads, Optionals, all kinds of pipelines (like map-reducers), transducers.
The text was updated successfully, but these errors were encountered: