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
Update several properties of a single object #1449
Comments
See #77 and #803, and the championed #162 (https://github.com/dotnet/csharplang/blob/master/proposals/records.md#with-expressions). |
Indeed, not easy to find if there is a relevant issue corresponding :) However I prefer the use of a '.' than a keyword which does not look like C# syntax. As mentioned in #1434 it can be followed by a question mark '.?' to avoid null exceptions. Thanks for the links! |
@JeanCollas Certainly not easy! No problem. C# is adding many new keywords such as Now whether |
For what I have in mind, that kind of keywords in only filters/test, a bit like linq or SQL syntax, or could be declarative like Except for this case, not so many keywords are used for assignment shortcut/value handling, it is most of the time done by operators ( However I will still be fine of course if this is the final choice of the team, but I am afraid that C# gets too verbose/complicated instead of keeping it straight (like javascript syntax). The shorter is the code, the easiest to read it is, and keep the words for what really needs explanations (how is the method, how is the class, etc). To focus on the If you take a Actually to be more precise, I would not expect a new list with merged content, nor an update of the list, but a VIEW of the original list, with these two additional values, like a new presenter of the object with overridden properties. |
From what I see there: DavidArno/SuccincT#44 it is already planned as ' with ' syntax for C#8. So I guess any discussion is now just almost vain :D |
I do prefer the syntax proposed by @JeanCollas ... hope this is not too late to change our mind on the |
Please don't go trusting statements made by random folk on the internet, especially when that random folk is me! 😀
|
Isn't There are some other proposals around supporting initializer syntax on expressions to support factory methods. That would probably be very similar to this proposal, sans the dot operator. But the team have also expressed little interest in the various proposals to promote mutation of the current state of an instance so I'm not convinced that such proposals have much in the way of legs at the moment. |
@DavidArno I will take this advice seriously :D @HaloFour It is actually not really clear to me. After more reading, I guess you are right. In that case, I believe this |
It would be nice if this could work to initializers, and to methods. |
I see MS proved my point about trying to guess releases of the language. On the very same day that I suggest C# 7.3 might appear in a month or two, they release C# 7.3! 😆 |
@Makeman-from-Makeloft Thanks for your message. I am not sure this exactly answers my issue with is more about simplified syntax/code structure, but it is very interesting on patterns. There are several new points I do not master enough yet. The Check/Match pattern looks nice |
After a second read of this long post, I only half agree with this. It looks nice, but actually it induces some things similar to global variables / inline delegates that you actually cannot easily extract from the code. |
For those who weren't around earlier: The banning has nothing to do with the workarounds he is suggesting. Personally, like @JeanCollas, we've seen (and tried to discuss) some drawbacks with them, but we are happy for anyone to look at them and use them if they like them. |
Is there a place for a word like |
Isn't this sort've similar to a public class MyLink
{
public string Name { get; set; }
public string Description { get; set; }
public string Link { get; set; }
public MetaData Meta { get; set; }
public void DrawLink() { }
}
...
public void UpdateLink(MyLink link, string name, string description, DateTime lastUpdate)
{
with link
{
Name = name;
Description = description;
with Meta
{
LastUpdate = lastUpdate;
}
DrawLink();
}
} Basically, the Regardless of if it uses |
@AustinBryan I agree. Also, just a thought: instead of |
@AustinBryan I agree this is similar to public string Name = "alpha";
public void T()
{
var s=new S() { Name = "beta" };
with(s)
{
Name = "gamma";
}
Console.WriteLine(this.Name);
Console.WriteLine(s.Name);
} That is why I think this should not be treated as scope, but only as shortcut public void UpdateLink(MyLink link, string name, string description, DateTime lastUpdate)
{
link.{
Name = name,
Description = description,
Meta.{
LastUpdate = lastUpdate
}
};
} would be translated: public void UpdateLink(MyLink link, string name, string description, DateTime lastUpdate)
{
link.Name = name;
link.Description = description;
link.Meta.LastUpdate = lastUpdate;
} which seems quite obvious. |
@JeanCollas I agree. So a few questions, would you be able to call methods too, from within the block? I would think so. And would What I like about this syntax is that it behaves consistently with if (b)
{
// do stuff
} I think, "just make it simpler and do this: if (b)
// do stuff And in your example, the braces for // braces
Meta.{
LastUpdate = lastUpdate;
}
// simplified
Meta.LastUpdate = lastUpdate; Without the braces it's just simply the original syntax, which I really like. If we used with (Meta) {
LastUpdate = lastUpdate;
}
// simplified
with (Meta) LastUpdate = lastUpdate; And that does the exact same thing as the normal way, so there's no point in there being more than one way. That just confuses people. |
I don't find any way to update several properties of a single object in C# (similar to the with VB statement).
It may be useful to clarify some mapping functions where several properties has to be updated at once.
I believe a
.
is a very concise and clear way to handle such case, and that it should not require an additional keyword for this functionality.Here is a very short example
Although it might induce some recursive conflicts.
The
UpdateLink
method would be translated by the compiler as:It can be used following factories calls
Only a
.
away from what people is already used to:It can easily handle null values to avoid exceptions:
The text was updated successfully, but these errors were encountered: