While proposals like this don't normally catch my attention, I think this one has some interesting bits to it.
For the purposes of my discussion, I assume something like the following would be legal:
package main
import "fmt"
type F func(y int) (x int)
func main() {
fmt.Println(F{x = y*y}(4)) // prints 16
}
This has the following rules:
- Input parameters must be named in the type for this to be usable.
- Iff the returns are named, an implicit bare "return" is assumed if the last statement is not a return or a panic
This has a number of interesting properties:
- Like most other such proposals, it dramatically reduces the amount of typing required for func literals.
- It enforces consistent naming of the parameters across all uses of the type
- Like structs when you use fields in your literals, adding a named param or return needn't cause a compile error
and might not require updating all references to the type, depending on the semantics
- Doesn't compromise type-safety, but still allows you to change e.g. size/signedness or use a named type in the
definition without being forced to also make the same change in dozens of places
The immediate problem with my flavor of his proposal is that statements are not allowed in composite literals syntactically, and F{} is a composite literal. I'm sure there are a number of ways around this syntactically, like making it func F{} or F(...){} (where the "..." is actually three dots, implying that something is being inferred in the same way as an array literal).