-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: spec: replace &T{} composite literals with (*T){} #20447
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
foo := struct{
p *int
}{
p: &i,
} |
If want to create new int value and get pointer of the value, we can't do it without |
But then what's the dereference operator? I think the most logical thing would be to use But I don't think it's worth it. |
If you are to replace &T{} with *T{}, I would be confused. I am used to seeing the pointer/deref (*) and the address of (&) operators. I think Go's usage of them has been consistent with other C-family programming languages. If you are to replace those, you will create unnecessary confusion to many people, considering the popularity of C/C++ and the people's familiarity with them. I don't agree with this proposal. |
Note that in your example, the lines st: struct{},
p: &struct{}, must today be written as st: struct{}{},
p: &struct{}{}, (two pairs of braces each). I suspect that, to avoid a parsing ambiguity, the latter case would need to be written as p: (*struct{}){}, but today that syntax is rejected by the compiler (https://play.golang.org/p/6GxjWrCfgN). |
Also note that under #12854 they could be written even more uniformly: foo := struct{
m map[int]string
a [10]int
sl []string
st struct{}
p *struct{}
}{
m: {},
a: {},
sl: {},
st: {},
p: {},
} |
This idea was discussed during the development of Go 1 (that is, as a change from the earlier versions of Go to the standarized Go 1), but was rejected at that time. |
It is important to understand that Perhaps memorizing this would help you stop confusing This also explains @cznic's comment: what if you want to point to a variable that already exists in a composite literal? (The line that is missing from their comment is |
@andlabs The
|
@bcmills Yes, but is |
The way I read this proposal, it is that |
Ah, yes, that is different. If we do expand composite literals to all types (a more general version of your reading of the proposal), by current rules it would have to be Isn't the current situation due to ambiguity between the binary |
&T{}
composite literals with *T{}
.&T{}
composite literals with (*T){}
&T{}
composite literals with (*T){}
The use of
&T{}
for composite literals is strange, and in particular inconsistent with the way you write a type. Consider the following code:p
is the only member for which the type declared does not match the type used in the composite literal. The logical fix is to make a composite literal of type*T
mean a composite literal to a pointer to a new value of typeT
initialized from the literal, in place of&T
.This is probably incompatible with #20446.
The text was updated successfully, but these errors were encountered: