Skip to content
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: Allow omitting types in callbacks if they can be inferred from the parameters #65369

Closed
3 of 4 tasks
wasserholz opened this issue Jan 30, 2024 · 3 comments
Closed
3 of 4 tasks
Labels
LanguageChange Suggested changes to the Go language Proposal v2 An incompatible library change
Milestone

Comments

@wasserholz
Copy link

wasserholz commented Jan 30, 2024

Go Programming Experience

Intermediate

Other Languages Experience

JS, Java, Python, Delphi, C

Related Idea

  • Has this idea, or one like it, been proposed before?
  • Does this affect error handling?
  • Is this about generics?
  • Is this change backward compatible? Breaking the Go 1 compatibility guarantee is a large cost and requires a large benefit

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

#21498
This issue is more general in how anonymous functions should behave, but it is not clearly defined in that proposal what should be done.

Does this affect error handling?

No

Is this about generics?

No, this is about inferring types from context in anonymous functions.

Proposal

It is possible to omit types for parameters in anonymous functions when the types can be inferred from the context.

Return type can be omitted because it can be inferred from the returned value in the anonymous function.
Parameter types are omitted as they can be inferred from the outer function declaration.

Language Spec Changes

Anonymous functions used as parameters in other functions can be written without re-declaring the parameter and return types, making anonymous functions more concise.

Informal Change

While using anonymous functions as parameters for other functions, the type definitions for the parameters and return values can be omitted, because the can be inferred from the context.

Is this change backward compatible?

Yes.

Given these functions:

func Map[T, V any](slice []T, accessor func(value T, index int) V) []V {
	output := make([]V, len(slice))
	for i, v := range slice {
		output[i] = accessor(v, i)
	}
	return output
}

func MapIntToString(slice []int, accessor func(value int, index int) string) []string {
	output := make([]string, len(slice))
	for i, v := range slice {
		output[i] = accessor(v, i)
	}
	return output
}

Their usage would currently looks like this:

func main() {
	nums := []int{1, 2, 3, 4}
	strings1 := Map(nums, func(v int, i int) string { return fmt.Sprintf("number: %v", v) })
	log.Print(strings1)

	strings2 := MapIntToString(nums, func(v int, i int) string { return fmt.Sprintf("number: %v", v) })
	log.Print(strings2)
}

Expected usage with this proposal:

func main() {
	nums := []int{1, 2, 3, 4}
	strings1 := Map(nums, func(v, i) { return fmt.Sprintf("number: %v", v) })
	log.Print(strings1)

	strings2 := MapIntToString(nums, func(v, i) { return fmt.Sprintf("number: %v", v) })
	log.Print(strings2)
}

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

No response

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

No response

Cost Description

No response

Changes to Go ToolChain

No response

Performance Costs

No response

Prototype

No response

@wasserholz wasserholz added LanguageChange Suggested changes to the Go language Proposal v2 An incompatible library change labels Jan 30, 2024
@gopherbot gopherbot added this to the Proposal milestone Jan 30, 2024
@zephyrtronium
Copy link
Contributor

This seems to be the same as @earthboundkid's recent suggestion in #21498. As described there, the proposed syntax already has a meaning, so this would break code that is valid today.

@earthboundkid
Copy link
Contributor

Yes, unfortunately, I don't think this is possible without changing the meaning of existing code.

@ianlancetaylor
Copy link
Contributor

Closing as dup of #21498, which covers the same idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
LanguageChange Suggested changes to the Go language Proposal v2 An incompatible library change
Projects
None yet
Development

No branches or pull requests

5 participants