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: spec: direct access to tuple return values #60086

Closed
AdeLuigi opened this issue May 9, 2023 · 3 comments
Closed

proposal: spec: direct access to tuple return values #60086

AdeLuigi opened this issue May 9, 2023 · 3 comments
Labels
LanguageChange Proposal v2 A language change or incompatible library change WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Milestone

Comments

@AdeLuigi
Copy link

AdeLuigi commented May 9, 2023

Currently, in Go, it is necessary to first assign the tuple returned by a function to a variable and then access the desired value from the variable. While this approach is readable and explicit, in some cases it may be convenient to directly access a specific return value from a function without having to assign it to a variable first.

For example, instead of writing:

tuple := returnTuple()
anyFunction(tuple[1])

It would be possible to write:

anyFunction(returnTuple()[1])

This more concise syntax would be especially useful when working with functions that return fixed-size tuples and whose return values are well-known. By allowing tuple values to be accessed directly from the function call, this change could improve the readability and clarity of Go code in some cases.

However, it is important to note that this change may make code less explicit in some cases, and may be harder to understand for developers less familiar with the Go language. Additionally, this proposal may be incompatible with the philosophy of the Go language to emphasize code readability and clarity over conciseness.

@ianlancetaylor
Copy link
Contributor

For language change proposals, please fill out the template at https://go.googlesource.com/proposal/+/refs/heads/master/go2-language-changes.md .

When you are done, please reply to the issue with @gopherbot please remove label WaitingForInfo.

Thanks!

@ianlancetaylor ianlancetaylor added v2 A language change or incompatible library change WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. LanguageChange labels May 9, 2023
@ianlancetaylor ianlancetaylor changed the title Direct Access to Tuple Return Values in Go Functions, Tuple()[1] proposal: spec: direct access to tuple return values May 9, 2023
@gopherbot gopherbot added this to the Proposal milestone May 9, 2023
@apparentlymart
Copy link

apparentlymart commented May 12, 2023

I feel a little unsure what exactly this proposal means by "tuple". Given the use of indexing syntax I guessed an array, but I don't think that's a correct guess because in that case both of the presented examples compile and are functionally equivalent:

package main

import "fmt"

func returnTuple() [2]string {
	return [...]string{"a", "b"}
}

func anyFunction(v string) {
	fmt.Printf("anyFunction(%#v)\n", v)
}

func main() {
	tuple := returnTuple()
	anyFunction(tuple[0])

	anyFunction(returnTuple()[0])
}

(Playground)

The Go spec uses "tuple" only in the concept of "tuple assignment" to refer to the idea of assigning multiple return values to multiple symbols, and so I suspect that's probably what you actually meant but note that the first example you provided is not correct if that's the interpretation you intended, because if would be forbidden to assign two function results to a single symbol like that:

package main

import "fmt"

func returnTuple() (string, string) {
	return "a", "b"
}

func anyFunction(v string) {
	fmt.Printf("anyFunction(%#v)\n", v)
}

func main() {
	// Error: assignment mismatch: 1 variable but returnTuple returns 2 values
	tuple := returnTuple()
	anyFunction(tuple[0])
}

I suspect this'll be clearer once you've completed the language changes template! 😀 But if your intent is to propose making multiple return values be usable as a single value of some new tuple type then you may wish to refer to #32941 first to consider how your proposal differs from that earlier one on a similar topic. There are some other related proposals linked later in the history of that issue which you may wish to consider too.

@gopherbot
Copy link

Timed out in state WaitingForInfo. Closing.

(I am just a bot, though. Please speak up if this is a mistake or you have the requested information.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
LanguageChange Proposal v2 A language change or incompatible library change WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

4 participants