Skip to content

proposal: Go 2: spec: shorter, type-inferred anonymous function syntax with usage limited to function arguments #47358

@DeedleFake

Description

@DeedleFake

Would you consider yourself a novice, intermediate, or experienced Go programmer?

Experienced.

What other languages do you have experience with?

C, Java, Kotlin, Ruby, Python, JavaScript, and some others, all to varying degrees.

Would this change make Go easier or harder to learn, and why?

Slightly harder, probably, but not by much, I think.

Has this idea, or one like it, been proposed before?

Yes. I can't seem to find them, but there have been proposals for similar syntaxes before. However, I believe that there are two key differences between those proposal and this:

  • The circumstances have changed due to generics being so close.
  • This proposal is much more restricted in what it allows, which I believe removes some of the concerns with the previous proposals.

Who does this proposal help, and why?

Anyone trying to call functions that take a simple function as an argument.

What is the proposed change?

The proposal is for a short syntax for anonymous functions. Currently, all functions must be fully typed, which can lead to some awkward code. For example, take the new proposed slices package:

// Pointless usage of EqualFunc() instead of Equal(), I know.
slices.EqualFunc(s1, s2, func(v1, v2 int) bool {
	return v1 == v2
})

The explicit typing makes calling this type of function a lot bulkier than it needs to be, and I think that it hurts readability as I result. Despite the type inference on the type parameter for EqualFunc(), the function signature of its third argument must be explicitely typed at the call site if using an anonymous function in this way. This example is very simple, but more complicated code could easily get very awkwardly difficult to read.

I propose that a short syntax be added that is only available in an argument to a function call. What the specifics of the syntax are can be debated, but as an example with some syntax borrowed, somewhat, from Kotlin:

// One-line function example:
slices.EqualFunc(s1, s2, { v1, v2 -> v1 == v2 })

// Multi-line function example:
slices.EqualFunc(s1, s2, { v1, v2 ->
	d := v1 - v2
	return (d < 3) && (d > 3)
})

Again, syntax isn't too important, but whatever syntax was used needs to support listing arguments without types. I think that it's also a good idea if it allows returns without the return keyword, at least in shorter, single-expression functions.

Is this change backward compatible?

Yes, fully. The example syntax above could potentially conflict with #12854 in some cases.

What is the cost of this proposal? (Every language change has a cost).

Minor increase to language complexity from an understanding point-of-view. Potentially larger effect on compilation, as it would require some amount of type inference backwards from the usage of the value. Due to the limited nature of it, however, I believe that it would not be too problematic. Run-time cost is non-existent, as this is 100% syntax sugar for an existing feature.

gofmt would have to be updated to support a nice formatting for the new syntax, such as inserting spaces in between the { and the arguments in the example syntax.

How would the language spec change?

An additional section would have to be added to the section on function calls explaining the usage, and various syntax specifications would have to be updated to accomodate it.

Orthogonality: how does this change interact or overlap with existing features?

It improves the ergonomics of in-line anonymous function usage as arguments to other functions.

Is the goal of this change a performance improvement?

No. This has no run-time impact whatsoever.

Does this affect error handling?

Not directly.

Is this about generics?

Indirectly. Mostly no.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions