-
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: Go 2: Function alias #58897
Comments
I don't understand what you're proposing. Your first example shows a closure being assigned to a variable, but your proposed syntax changes seem to be for creating a top-level function that is an alias for another top-level function. If that's correct, than this can already be accomplished by just doing
|
Thanks for fix! func X(str string) string {
return func(s string) string {
return s
}()
} Like this? |
How does this proposal help with closures? |
I do wish this worked: package whatever
type fn func()
const f fn = func() {
// ...
} You have to use |
@carlmjohnson How is a "const func" different to just using a named func (which are effectively constant and can't be reassigned)? func f() {
// ...
} |
I thought the same thing and almost posted the same question, but there is actually one minor difference: Typing. A function declaration is always untyped, but a Instead of |
You can add methods to typed functions, but you can't add methods to "constant" functions: Not legal now: package whatever
type fn func()
func (fn) foo() {}
const f fn = func() {
// ...
}
func main() {
f.foo()
} Legal now: package whatever
type fn func()
func (fn) foo() {}
func f() { /* */ }
func main() {
fn(f).foo()
} |
This proposal helps make variable functions documentation better, closure is one example of many uses |
@xbt573 I'm sorry, I don't understand. Can you show us a small example? Thanks. |
First example: Reduce boilerplate We have code like this: package pkg
func A(str string) string {
return str + "A"
}
func B(str string) string {
return str + "B"
}
func C(str string) string {
return str + "C"
} Using function alias, we can make this code tidier: package pkg
type fn func(str string) string
func createFn(suffix string) fn {
return func(str string) string {
return str + suffix
}
}
// func A(str string) string
func A = createFn("A")
// func B(str string) string
func B = createFn("B")
// func C(str string) string
func C = createFn("C") Second example: Function ascension
package subpkg
type Dummy struct{}
func NewDummy() *Dummy {
return &Dummy{}
} But we want user to use this function at package package test
import "test/subpkg"
// func NewDummy() *subpkg.NewDummy
func NewDummy = subpkg.NewDummy If we use var A pkg.fn
var NewDummy func() *subpkg.NewDummy |
First example: I don't actually think the first example is "tidier" -- to me it seems (slightly) more abstract and harder to understand. For this example, I'd use the fact that func A(str string) string { return addSuffix(str, "A") }
func B(str string) string { return addSuffix(str, "B") }
func C(str string) string { return addSuffix(str, "C") }
func addSuffix(str, suffix string) string {
return str + suffix
} Second example: I agree this would be nice, and I've wanted it a few times. However, I'm not sure the additional syntax is worth it. It's very easy just to write: // NewDummy blah blah blah doc comment.
func NewDummy(args) *subpkg.Dummy {
return subpkg.NewDummy(args)
} It might be useful to see how often this kind of "trivial func wrapper" is done in a large corpus of Go code. |
type fn func(str string) string
func createFn(suffix string) fn {
return func(str string) string {
return str + suffix
}
}
// func A(str string) string
func A = createFn("A") This example seems to imply the Go compiler executing I don't think there's any other situation where the Go compiler currently executes a function at compile time quite like this, but perhaps I'm missing something. (Another possible interpretation that would avoid the compile time function evaluation would be to treat this as a shorthand for |
If I'm not mistaken, this function is executed at package import, here's example: https://go.dev/play/p/6bYKwjdBYiD |
@xbt573 No, that's happening at runtime, at package init time (spec reference). |
The benefits of this new syntax do not outweigh the significant costs of a language change. Also the emoji voting is not in favor. Therefore, this is a likely decline. Leaving open for three weeks for final comments. |
No further comments. |
Author background
Intermediate
Python, C#, JS/TS, Bash
Related proposals
Probably not
No
No
Proposal
This proposal adds something like type aliases, but for functions
If we take code like this:
And then check documentation for SomeFunction, it will show:
It is very disappointing at first time
This proposal adds new syntax:
If
Y
doc is:Then,
X
doc will be:New syntax, new chapter about function alias
Function alias helps create function type variables documentation easier
Yes
It doesn't
No
Costs
It can make Go a little bit harder to learn, but it will make documentation better in some cases
New syntax realization
gopls, gofmt (maybe)
Translating functions
None
No
No
The text was updated successfully, but these errors were encountered: