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: compact syntax for multiple property assignation on a same object. #35528

Open
Zenithar opened this issue Nov 12, 2019 · 12 comments

Comments

@Zenithar
Copy link

@Zenithar Zenithar commented Nov 12, 2019

Hello,

In order to make assignation more DRY, it would be great to have something like that :

type FooStruct struct {
  ID string
  Name string
}

var res FooStruct

res.(
  ID = "12345"
  Name = "Foo"
)

// To replace
res.ID = "123456"
res.Name = "Foo"

Syntax should obviously be enhanced, but just an idea.

@gopherbot gopherbot added this to the Proposal milestone Nov 12, 2019
@gopherbot gopherbot added the Proposal label Nov 12, 2019
@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Nov 12, 2019

Today you can already write

    res = FooStruct{
        ID:   "12345",
        Name: "Foo",
    }
@Zenithar

This comment has been minimized.

Copy link
Author

@Zenithar Zenithar commented Nov 12, 2019

No I don't want to declare struct and properties in a concise way. But really group assignation in order to remove res. each time.

@Zenithar

This comment has been minimized.

Copy link
Author

@Zenithar Zenithar commented Nov 12, 2019

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"
@Zenithar

This comment has been minimized.

Copy link
Author

@Zenithar Zenithar commented Nov 12, 2019

It is like accessing the struct properties by key/value. Like you could do in JS for example.

@randall77

This comment has been minimized.

Copy link
Contributor

@randall77 randall77 commented Nov 12, 2019

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.

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Dec 3, 2019

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.

@jimmyfrasche

This comment was marked as off-topic.

Copy link
Member

@jimmyfrasche jimmyfrasche commented Dec 3, 2019

If Go had immutable types/values something like this would be needed. Without those, it might be nice in some situations (like simulating immutability), but probably not enough to be worth the change.

@ianlancetaylor

This comment was marked as off-topic.

Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Dec 4, 2019

@jimmyfrasche Do you mean "would be needed" or "would not be needed"? I'm not quite seeing why it "would be needed".

@jimmyfrasche

This comment was marked as off-topic.

Copy link
Member

@jimmyfrasche jimmyfrasche commented Dec 4, 2019

I thought this was a different issue. Sorry for the confusion!

@bcmills

This comment has been minimized.

Copy link
Member

@bcmills bcmills commented Dec 4, 2019

Some languages in the ML family support struct assignment with a field update: for example, Rust has struct update syntax using the .. token, and OCaml has functional update notation using the with keyword.

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 ... token, by analogy to its use for slices:

	res := Foo{
		Name: "bar",
	}
	res = Foo{
		res...,
		ID: "1234",
		Name: "Foo",	
	}

or perhaps

	res := Foo{
		Name: "bar",
	}
	res = res...{
		ID: "1234",
		Name: "Foo",	
	}
@bcmills

This comment has been minimized.

Copy link
Member

@bcmills bcmills commented Dec 4, 2019

The second possibility above leads to a natural variant of the = operator, by analogy to the existing +=, >>=, &^=, […] variants:

	x.SomeDescriptiveFieldName ...= {
		ID: "1234",
		Name: "Foo",	
	}

could be a shorthand notation for

	x.SomeDescriptiveFieldName = x.SomeDescriptiveFieldName...{
		ID: "1234",
		Name: "Foo",	
	}
@Zenithar

This comment has been minimized.

Copy link
Author

@Zenithar Zenithar commented Dec 6, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
6 participants
You can’t perform that action at this time.