-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: spec: allow taking address of value returned in function #45653
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
Comments
Please fill out https://github.com/golang/proposal/blob/master/go2-language-changes.md when proposing language changes |
If |
This is related to #45624. |
Interesting. If it is possible to take the address of a field of the value returned by
Would they behave differently to the developer, or only to the runtime? My understanding is the behavior difference would be in the runtime, which seems less of an issue. What problems does this behavior difference create? |
In one case you have the only pointer to the value and in the other case you have a pointer that other code may also have. That's a pretty big difference when trying to understand the behavior of your code with multiple goroutines. For example, you know that |
Pointer expression to any value (including return of function) already exist. They are just ugly. Example (see on the Go playground): nowPtr := &(&struct{ time.Time }{time.Now()}).Time
fmt.Printf("%T %[1]v\n", nowPtr)
// Output: *time.Time 2009-11-10 23:00:00 +0000 UTC m=+0.000000001 |
@dolmen FWIW, a more concise (but still ugly) spelling would be |
Would this proposal affect slices as well? (I can file a separate proposal if this is best discussed on its own task.) Right now, one must do:
It'd be much more convenient to be able to write:
In Go 1.16, the above results in Behind the scenes, I would expect the compiler to automatically perform the copy of the return value to back the desired slice. Looking at the proposed
...although that seems much less clear to me. |
I would recommend that it does not. The fact that If the variant form |
@seankhliao Thanks for the link. Done! @gopherbot please remove label WaitingForInfo |
With generics, we could use func PtrTo[T any](v T) *T { return &v }
s := S{
Time: PtrTo(time.Now())
} to take the address of any value. |
Based on the discussion above this is a likely decline. Leaving open for four weeks for final comments. |
No further comments. |
Similar to @griesemer's proposal #3117 –
The non-addressability requirement of values returned from functions makes sense by itself since we don't want a surviving pointer to a value returned on the stack.
But surely it should be ok to have an invisible temporary variable copying the respective value which is just used during the assignment. It seems that this doesn't introduce any more issues than what we have already when assigning that cannot be assigned to atomically.
This would be particularly convenient when assigning the return value of functions to pointer fields in struct types that are used for JSON serialization. For better or worse the way to indicate struct types are empty is to make them a pointer, with omitempty, but this makes it inconvenient to directly assign values from functions that aren't pointers to those types.
E.g., the following program should be legal:
E.g., this is one example of the code required today to work around this limitation:
In a simple example this doesn't seem terribly inconvenient, but in large APIs it becomes tedious.
This proposal is constrained to single-value return functions, and not return functions return multiple values because functions that return multiple-values can already only be assigned to an explicit list of variables.
Proposal template:
Would you consider yourself a novice, intermediate, or experienced Go programmer?
Experienced
What other languages do you have experience with?
Java, Ruby, C#, C, JavaScript
Would this change make Go easier or harder to learn, and why?
A little easier.
Has this idea, or one like it, been proposed before?
Similar to proposal: spec: expression to create pointer to simple types #45624.
It refers to making simple types addressable, where this proposal refers to making values from return functions addressable.
Who does this proposal help, and why?
See above.
What is the proposed change?
See above.
Please describe as precisely as possible the change to the language.
See above.
What would change in the language spec?
The specification of what can be addressed, otherwise nothing.
Please also describe the change informally, as in a class teaching Go.
See above.
Is this change backward compatible?
Yes
Show example code before and after the change.
See above.
What is the cost of this proposal? (Every language change has a cost).
I'm not sure.
Little.
None.
Can you describe a possible implementation?
See above.
No.
How would the language spec change?
It wouldn't.
Orthogonality: how does this change interact or overlap with existing features?
It expands existing patterns of using & to address items.
Is the goal of this change a performance improvement?
No.
Does this affect error handling?
No.
Is this about generics?
No.
The text was updated successfully, but these errors were encountered: