Go Should Allow Implicit Typing on Funcs
I’d love to see Go implement implicit typing for function arguments.
Let's say for example, I'm sorting a slice of items by the created date:
sort.Slice(users, func(a, b int) bool {
return users[a].CreatedAt.After(users[b].CreatedAt)
})
The definition for sort.Slice
states that the second argument to Slice should be a function that takes two integers and returns a bool. So, why should I need to redefine those typings when I write a function that satisfies that?
Would be much neater to be able to:
sort.Slice(users, func(a, b) {
return users[a].CreatedAt.After(users[b].CreatedAt)
})
And only raise a compile error if the two arguments are used incorrectly (trying to access a property on one of the ints) or the function doesn’t return a bool.
Update: turns out there is a technical phrase for this: "eliding"
Many languages permit eliding the parameter and return types of the anonymous function in this case, since they may be derived from the context.
There's already a proposal to enable this functionality (combined with arrow syntax for anonymous functions), but the community seems pretty divided. Let's not hold out hope.
Comments ()