Suggest adding "with" statement to C# #1534
Replies: 37 comments 29 replies
-
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
@ufcpp I may have to *gasp* disagree with Jon Skeet on that. A That said, I like its current proposed form as an easier way to work with immutable objects. |
Beta Was this translation helpful? Give feedback.
-
It wasn't Jon Skeet's opinion, he was just reblogging this post from Scott Wiltamuth. With |
Beta Was this translation helpful? Give feedback.
-
Fair enough, and this proposal (and that link) addresses a |
Beta Was this translation helpful? Give feedback.
-
Scott Wiltamuth's arguments against var updatedCustomAction = MappingHelper.Map<DbCustomAction>(customAction);
updatedCustomAction.CreatedBy = existingCustomAction.CreatedBy;
updatedCustomAction.CreatedOn = existingCustomAction.CreatedOn;
updatedCustomAction.UpdatedOn = DateTimeOffset.UtcNow;
updatedCustomAction.UpdatedBy = currentUserId;
updatedCustomAction.DeletedBy = null;
updatedCustomAction.DeletedOn = null; versus var updatedCustomAction = MappingHelper.Map<DbCustomAction>(customAction);
with(updatedCustomAction)
{
.CreatedBy = existingCustomAction.CreatedBy;
.CreatedOn = existingCustomAction.CreatedOn;
.UpdatedOn = DateTimeOffset.UtcNow;
.UpdatedBy = currentUserId;
.DeletedBy = null;
.DeletedOn = null;
} which is a lot easier to read in my opinion. |
Beta Was this translation helpful? Give feedback.
-
@bondsbw you're right, I somehow got the wrong issue number. I meant #803, though #77 and #162 are more closely related to your specific examples, I think. |
Beta Was this translation helpful? Give feedback.
-
Sugestion
|
Beta Was this translation helpful? Give feedback.
-
Before I offered same 'with' operator and now also support rushfrisby's offer. It's so obvious simplification of code that it's surprise to me people didn't implement it yet, but discuss "how to make total mess, introducing 'class ABC (some variables)' rubbish". Above just "votes" C# team should use brains to filter really helpful features. When they made object initializers, THAT was "almost WITH operator", but half-baked due to narrow vision. Now we again and again ask for absolutely simplistic thing - introduce "with" statement which will clean-up boilerplate code. |
Beta Was this translation helpful? Give feedback.
-
The C# team hasn't offered My opinion is that the |
Beta Was this translation helpful? Give feedback.
This comment was marked as disruptive content.
This comment was marked as disruptive content.
-
It's not a goal of C# to make the language as terse as possible. The difference in verbosity between a proposed Even if this proposal was less verbose by a noticeable degree, that's still not enough of a reason for it to be added to the language. It has to fit in to the design goals of the language design team and be deemed worth the massive amount of time and effort to implement and support for perpetuity.
You're free to use those languages. MS even ships one.. |
Beta Was this translation helpful? Give feedback.
This comment was marked as disruptive content.
This comment was marked as disruptive content.
-
First you state:
The you state
These statements seem to be in contradiction to each other. First you suggest the C# designers should not listen to what developers ask for and should make their own decision, then you suggest that they should not make their own decisions but should do what people ask them. Which is it? Regardless this request has neither votes, nor backing by the LDM, so either way is unlikely to happen. |
Beta Was this translation helpful? Give feedback.
-
@HaloFour Related "INotifyPropertyChanged" - just trust me, 10 years later people will kill you for any mention of that ugliest "programmer's invention" (like now for 'goto'). Sorry for offtop. |
Beta Was this translation helpful? Give feedback.
-
Can you please try and avoid being impolite when discussing things here. It is making this conversation unpleasant. |
Beta Was this translation helpful? Give feedback.
This comment was marked as off-topic.
This comment was marked as off-topic.
-
This is just rehashing this conversation: dotnet/roslyn#6553 |
Beta Was this translation helpful? Give feedback.
-
@WrongBit, I don't see why this can't be solved with some previously suggested library method to let you give it a nice scoped short name. public static void With<T>(this T foo, Action<T> action) => action(foo); no need to yell at people. {
var s = SomeAwfulEldritchUnspeakablyLongName;
s.Prop1 = "";
...
s.PropN = "";
} I don't think this situation is very common. Usually, I either initialize and modify after newing up, and in the rare cases I can't because it is given to me by IoC, I rather fix those cases by configuration rather than in code. |
Beta Was this translation helpful? Give feedback.
-
To give you some context. I was a member of the C# language design group for over a decade. I helped create C# versions 2 through 8. So i've got a lot of experience and thoughts about this. First off, your attitude and communication style aren't doing you any favors. At best, they are turning people just not want to listen to you. At worst, it is a violation of the rules of conduct of github and the dotnet foundation. You need to change this, or you will get banned from participating here.
Then walk away. You're doing yourself no favors by arguing with your emotions. The most gentle way i can put this is that it makes you look petulant and immature. Everyone here is passionate. But people are far more effective when tehy can channel their emotions in a healthy fashion. Now, ignoring your communication style and just focusing on your opinions:
Simplicity is often in the eye of the beholder. One person's simplicity is another's complexity. See, for example, how the C++ community views simplicity, and how that differs from the Go community's views. It is erroneous to assume that your own perspective on what is simple is universally true across the ecosystem and community that C# targets. For example, some people find things simpler the less they need to look around to pull in necessary context. With For example, say i have this:
versus
That's the difference of one token, but it vastly changes the meaning of thins. Previously i could look at the first one and know that -- Finally, please consider that some people here have been doing this a long time, and have a lot of experience trying to figure out the particular pros/cons of a proposal and if they would be net positive for the language. Many will disagree with you. If you want conversations to proceed toward your desired ends you would do well to work with those that disagree, instead of being hostile to them and lashing out. Cheers, and good luck! |
Beta Was this translation helpful? Give feedback.
-
If the name of the identifier is so important that you feel that you need to give it a "long meaningful name" then you'd likely not be using a feature like I don't understand your example of assigning a variable within the
Ignoring the arguments you don't like doesn't mean that they don't exist. But more critically, you'd need serious arguments for the proposal before the language team would consider adding such a feature. They've clearly known about As for the tone, I believe that I've been civil towards you even if I've been opinionated towards the feature. I'd appreciate responses in kind. If a rebuttal of your argument can only be seen as "childish" there's little chance that we can actually have a meaningful conversation here. |
Beta Was this translation helpful? Give feedback.
-
@CyrusNajmabadi |
Beta Was this translation helpful? Give feedback.
This comment was marked as disruptive content.
This comment was marked as disruptive content.
-
Thanks to @DanFTRX he found my old request from 2015, because @rushfrisby miss one important thing we can do, having "with" - adding delegate! My sample:
Funny, but "serious guys" miss this point with ".Event" and we got half-baked "object initializer", which do NOT allow assign event! My proposal (dotnet/roslyn#6553) fixes this mistake + adds ability to easy assign properties not only after object creation (again limitation!), but anywhere in a program. |
Beta Was this translation helpful? Give feedback.
-
I was 100% serious with my critique.
It's been an argument since v1. We think strongly about these scenarios and the impact it has on customers. Furthermore, the LDM has the absolute final say here. So you need to operate within the perspectives and considerations that hte LDM members have here. I laid out a trivial example of how this language feature makes things more complex. I now cannot trivially tell what i brought this up not to indicate that the feature shuld not be be done, but to help you with perspective. Your perspective is that this is strictly simpler. However, i showed a completely reasonable perspective that demonstrates the additional complexity added. You need to be able to see things through a multitude of perspectives if you want to be able to convince others of the value of these features.
Let me make this as clear and as blunt as possible. I decided to reach out to try to help you here. If you're going to behave in a childish and petulant manner, then i'll simply walk away as i have no interest engaging in people who behave this way. You will then lose someone who could have been an advocate for your position. So ask yourself this: "What am i trying to accomplish?" If your goal is to get this language proposal accepted, you are burning bridges and doing a great job to turn people against it entirely based solely on your behavior. However, if on hte other hand, your objective is to feel good about yourself and think you can browbeat people into supporting you, you can continue in this fashion. But i can assure you, there has been a 0% success rate with getting the language changed by people behaving in this manner in the past. So what are you trying to accomplish? And how will you change your behavior to have a better chance to get what you want. -- To help you out, i'm willing to respond one more time. It will be based on the behavior and attitude of your next posts. If they are rude and childish, I'll let you know and simply walk away. If they demonstrate a willingness to engage and work with others in a civil and adult fashion, I'll be happy to keep communicating and sharing my several decades of experience working directly in this space. It's up to you how you proceed. |
Beta Was this translation helpful? Give feedback.
-
Let's assume you did have a With statement. Cyrus's example is interesting as he shows a potential error vector. Let's simplify it with(f){
.Prop = SomeChained()
.Calls();
} now if I accidentally introduce a semicolon with(f){
.Prop = SomeChained();
.Calls();
} These segments may both be valid C#. In the first we are assigning This reminds me of the danger of semicolon completion in javascript, and this would immediately make me not want to use this method at all. The question when it comes to language feature is not "Why Not?". It needs to be shown as to why it is a large enough value add to be worthwhile. Everyone writes properties, and trimming down the noise to |
Beta Was this translation helpful? Give feedback.
-
Temporary variables are not dirt. They are the idiomatic way to approach a problem such as this in many languages. They are how |
Beta Was this translation helpful? Give feedback.
-
I also don't like the proposed Here a small example. Suppose we have a class that holds the minimum, maximum and the mean value of a data series plus some decriptive properties. public class ValueStatistics
{
public ValueStatisitics() {}
public ValueStatisitics(IEnumerable<double> series) { ... }
public double Minimum { get; set; }
public double Maximum { get; set; }
public double Mean { get; set; }
public string ValueName { get; set; }
public string Unit { get; set; }
public string Location { get; set; }
} Now we have a property that returns the statistics for all values in a list: public IEnumerable<ValueStatistics> StatisticallyValues => AllValues
.GroupBy(v => v.ValueName)
.Select(g => new ValueStatistics(g.Select(v => v.Value))
{
ValueName = g.Key,
Unit = g.First().Unit
}); It turns out that some values are very off and we don't want to consider those outliers in the statistics. So we write a factory method for public IEnumerable<ValueStatistics> StatisticallyValues => AllValues
.GroupBy(v => v.ValueName)
.Select(g =>
{
var s = ValueStatistics.WithoutOutliers(g.Select(v => v.Value));
s.ValueName = g.Key;
s.Unit = g.First().Unit;
return s;
}); So we have to edit several lines (adding With the second proposed syntax it'd be only one or two line changes. public IEnumerable<ValueStatistics> StatisticallyValues => AllValues
.GroupBy(v => v.ValueName)
.Select(g => ValueStatistics.WithoutOutliers(g.Select(v => v.Value)).
{
ValueName = g.Key,
Unit = g.First().Unit
}); The only problem is that the dot doesn't really fit into the Allman style. On the other hand I have the impression that the |
Beta Was this translation helpful? Give feedback.
-
@HaloFour @WrongBit @CyrusNajmabadi
And so, instead of this proposed feature that could be universally used in a number of different contexts, we have object initializers and the new |
Beta Was this translation helpful? Give feedback.
-
This would work similar to the VB With statement
The syntax in C# could look something like:
alternatively it could look like:
or both ways!
This seems reasonable seeing as how there is already some behavior like this for object initialization.
Beta Was this translation helpful? Give feedback.
All reactions