-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: compact syntax for multiple property assignation on a same object. #35528
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
Today you can already write res = FooStruct{
ID: "12345",
Name: "Foo",
} |
No I don't want to declare struct and properties in a concise way. But really group assignation in order to remove |
Given this state : type Foo struct {
ID string
Name string
Bar string
Whatever uint64
}
res := Foo{
Name: "bar"
} Applying this res.{
ID = "1234",
Name = "Foo",
} Is an alias of this : #SyntacticSugar res.ID = "123456"
res.Name = "Foo" |
It is like accessing the struct properties by key/value. Like you could do in JS for example. |
This does not seem like it is worth modifying the language for. The assignments you want to replace are fine as is. DRY is IMO overkill when the thing you are repeating is the text of a single variable. |
This proposal does not have much support according to emoji votes. As @randall77 says the only repetition saved is relatively small. For these reasons, this is a likely decline. Leaving open for four weeks for final comments. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Some languages in the ML family support struct assignment with a field update: for example, Rust has struct update syntax using the Something more like that seems like it would address not only this use-case, but perhaps also some related ones (such as #33957). For Go, I would be inclined to use the res := Foo{
Name: "bar",
}
res = Foo{
res...,
ID: "1234",
Name: "Foo",
} or perhaps res := Foo{
Name: "bar",
}
res = res...{
ID: "1234",
Name: "Foo",
} |
The second possibility above leads to a natural variant of the x.SomeDescriptiveFieldName ...= {
ID: "1234",
Name: "Foo",
} could be a shorthand notation for x.SomeDescriptiveFieldName = x.SomeDescriptiveFieldName...{
ID: "1234",
Name: "Foo",
} |
Exactly ! And the functional update object could be a struct, so that you could merge struct with same syntax. patch := struct{
Name string
}{
Name: "foo"
}
x.SomeDescriptiveFieldName ...= patch It's like using mergo but integrated in language. |
@Zenithar, I don't think the RHS could be allowed to be a variable. That would be too ambiguous. If I write: patch := struct{
ID string
Name string
}{
ID: "foo",
}
x.SomeDescriptiveFieldName ...= patch should that set For clarity, it seems to me that the thing being mixed in must always be a literal. |
No change in consensus on this specific proposal. |
Hello,
In order to make assignation more DRY, it would be great to have something like that :
Syntax should obviously be enhanced, but just an idea.
The text was updated successfully, but these errors were encountered: