-
Notifications
You must be signed in to change notification settings - Fork 17.6k
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: treat declared function names as type names #35535
Comments
The var f typeof(Filter) = func(x int) bool {
[…]
} or var f Filter.(type) = func(x int) bool {
[…]
} |
At https://blog.golang.org/go2-here-we-come Robert laid out some guidelines for language changes, including that a change should "address an important issue for many people." It's not clear to me that this meets that bar. I'm not even sure when someone would want to use this feature. |
You can already do this with the existing language features, like this example: package main
import (
"fmt"
)
type Filter func (int) bool
func (f Filter) Apply(in []int) []int {
res := []int{}
for _, v := range in {
if f(v) {
res = append(res, v)
}
}
return res
}
func main() {
arr := []int{7, 9, 3, 5}
var filter Filter = func(i int) bool {
return i > 6
}
res := filter.Apply(arr)
fmt.Printf("arr -> res: %v -> %v\n", arr, res)
} https://play.golang.org/p/MmcSA6FwlbV So yes, you need to write a function type definition and a function that matches it separately, but It would be confusing for a function definition to also be an implicit type definition. |
I forgot to mention another benefit of this proposal. package foo
func ErrXXX() {}
func (ErrXXX) Error() string {
return "ErrXXX"
} |
This proposal doesn't seem to have much support and doesn't seem to solve an important problem. The suggestion from @bcmills seems more like Go if we want to make any changes in this direction. The comment above suggests that |
There were no further comments. |
so that function names can be used as embedded types:
Another benefit.
Not an essential feature, but it is a little interesting, so I created the proposal.
The text was updated successfully, but these errors were encountered: