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

Shorthand to update multiple fields of an object at once (similar to simple object creation) #3364

Closed
audigex opened this issue Apr 17, 2020 · 3 comments

Comments

@audigex
Copy link

audigex commented Apr 17, 2020

I think everyone is familiar with the nice simple "create a new instance and populate some fields" syntax, it's nice, it's clean, it keeps related code together, and I've never found anyone who doesn't love it... for example:

foo = new Bar()
{
    BarID = id,
    Value = 123,
    Title = "Something"
};

That's great when we're creating foo while setting the values.

But now let's assume that foo already exists, so we can't create it using the above syntax, but we still want to update some fields. It feels like this should be just as easy to do and with similar syntax, but the following isn't possible:

foo {
    BarID = id,
    Value = 123,
    Title = "Something"
};

Instead we have to resort to the old, ugly version:

foo.BarID = id;
foo.Value = 123;
foo.Title = "Something";

Hopefully my request is self explanatory: allow us to use that nice clean syntax when updating fields on an existing instance of an object, not just when creating the object.

@hez2010
Copy link

hez2010 commented Apr 17, 2020

With records you can approach this with below code:

foo = foo with {
    BarID = id,
    Value = 123,
    Title = "Something"
};

@jnm2
Copy link
Contributor

jnm2 commented Apr 17, 2020

Work is underway for a feature that uses the syntax foo with { BarId = id } to do a non-destructive update (returning a new value). See #162.

@YairHalberstadt
Copy link
Contributor

Closing as duplicate of #803 and #1534

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants