-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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: currying functions with f{x}
and f{arg: x}
#66184
Comments
This proposal break the assumption that arguments names are not part of the API. It's a backward compatible change, but I don't know if we want something like that. As an example of why adding meaning to arguments of function looks weird to me: otherPackage.Foo{b: 6} shouldn't be allowed imo, for consistency with everything else that use named keys. I think it's equally weird if we apply public / private rules to argument because what would this do: // otherpackage.go
func Foo1(B int) {}
func Foo2(b int) {}
// package.go
var a func(b int) = otherPackage.Foo1 // this shouldn't probably fail
var b = a{b: 52}
var c func(B int) = otherPackage.Foo2 // should this fail ? it doesn't currently, and would be breaking change, but it takes a private argument and make it public
var d = c{B: 52} // should this fail ? should the `c =` assignement remember if the original function had public or private arguments, but that a runtime fail We would also need a syntax like python's Adding argument names to the signature of the function looks like a big can of worms to me, and the value of your proposal is very limited (some syntax sugar), I think this proposal shouldn't be accepted without a more complete story for named arguments as part of the api (like |
It's called currying, and it's a very important concept in functional programming, but I don't think the Go team would choose to introduce them. |
f{x}
f{x}
f{x}
and f{arg: x}
In Elixir, there's a 'capture operator', I don't think it would make sense to overload the I'm not necessarily advocating this, but this approach might be cleaner. It separates it from the argument names this way. |
This amounts to syntactic sugar, since you can already use a function literal. |
Based on the discussion above, and the emoji voting, this is a likely decline. Leaving open for four weeks for final comments. |
No further comments. |
Go Programming Experience
Intermediate
Other Languages Experience
No response
Related Idea
Has this idea, or one like it, been proposed before?
No
Does this affect error handling?
No
Is this about generics?
No
Proposal
Allow to construct function composite literal, inside or outside function, to provide a convenient way of constructing named function out of existing function in the case of the only difference is that some arguments differ
Language Spec Changes
Treat function name as type in the case of composite literal
Informal Change
Example:
Is this change backward compatible?
Yes
Orthogonality: How does this change interact or overlap with existing features?
No
Would this change make Go easier or harder to learn, and why?
Could understand easily, the concept is well known
Cost Description
Complicate the difficulty of distinguishing function as value or type
Changes to Go ToolChain
Not familiar
Performance Costs
No
Prototype
Types:treat function as type in the case of composite literal
Noder/writer:write down base function object info and fixed arguments
Noder/reader:construct the closure accordingly
The text was updated successfully, but these errors were encountered: