[Proposal] Allow set operator when calling a method => ".=" #500
Replies: 25 comments 2 replies
-
var now = DateTime.Now;
var thisTimeTomorrow = now.AddDays(1);
var list = ...;
var listSortedByPosition = list.OrderBy(x => x.Position); Why overload a single variable with multiple meanings? If you create a now value, it has a new meaning, so give it a new name. I am opposed to suggestions like |
Beta Was this translation helpful? Give feedback.
-
1: I didn't call my variable now |
Beta Was this translation helpful? Give feedback.
-
A problem can occurred if the output format of the method is different than the format of the variable, like this case :
In this case, s.=ToArray(); will throw a CS0029 compilation error. But you can do the same error today :
And the same result, a CS0029 compilation error, is a good behavior in both cases. |
Beta Was this translation helpful? Give feedback.
-
var match = Regex.Match(input, pattern);
while(match.Success)
{
Foobar(match);
match .= NextMatch();
} |
Beta Was this translation helpful? Give feedback.
-
foreach (var match in new Regex(pattern).Matches(input))
{
Foobar(match);
} Which more than adequately illustrates my point. @GeirGrusom's Instead, if one stops and thinks "can I avoid mutating |
Beta Was this translation helpful? Give feedback.
-
Yes, I think that in many discussions here the participants fixate too much on the concrete samples given insead of seeing them only as one potential case study. There needn't always be a very concrete RL scenario by the time the discussion/proposal is made. Many times the usefulness comes with the feature when you start using it and say "hey, didn't know that I was looking for it but it feels good" 😃 For issues named "Feature Request", as being stated as a concrete 'request', I would expect a real world practical use case scenario where the practical benefit for the specific situation is described. About the proposalI consider it inexperience to run into those kinds of mistakes, no offense! We all have our habitudes (human factors, bla bla bla... 😁 ) . But this time I have to concur with @DavidArno that I perceive it as a dangerous construct that really promotes mutability. Rather than treating the symptom, in this case the cause should be resolved. |
Beta Was this translation helpful? Give feedback.
-
@AdrienTorris Don't give up on it too quickly 😃 You only had 3 reactions out of 100s. Maybe the other 97 vote in favor of this? You never know. It would be nice to have a final comment from you, why you revoked this proposal and closed it. One argument against the chosen operator syntax. |
Beta Was this translation helpful? Give feedback.
-
@lachbaer I closed this issue because you guys seems unanimous about the low interest of this issue, but I just want to be constructive. If 100s people take the time to read this issue and loose their time, it's couter productive. I never used PHP so I didn't know that this operator already exists in this language. I chose this syntax because I found it readable and instinctive. The equal in second position already exists in C# ( About the mutability issue you speak about, I find this point very interesting but I don't really see the problem here. The mutability of an object is managed by the Framework so I don't see the bad thing about promoting mutability. When you use the I tried to find a really good real-world example to illustrate this issue and show how this operator could be usefull and more than a little syntaxic-sugar item, and I didn't find one. |
Beta Was this translation helpful? Give feedback.
-
Fyi you're mutating the variable from pointing to one immutable string to pointing to a new immutable string.
I agree with you. C# is multi-paradigm and the mutable and procedural paradigms are on equal footing with the immutable and functional paradigms. I have this crazy idea that C# should continue to be really good at both. ;-) |
Beta Was this translation helpful? Give feedback.
-
@DavidArno what surprised me is that |
Beta Was this translation helpful? Give feedback.
-
I use zero-length matches all the time. I didn't think this was how the Regex engine worked, so I tried to create an infinite loop with your code and cannot. Can you? |
Beta Was this translation helpful? Give feedback.
-
I don't see any difference between the call the |
Beta Was this translation helpful? Give feedback.
-
Hmmm I could swear that I got an inifinite loop the other day, but perhaps I just didn't assign |
Beta Was this translation helpful? Give feedback.
-
@AdrienTorris I really want to second @lachbaer and urge you to reopen this proposal. In addition, I want to add that
|
Beta Was this translation helpful? Give feedback.
-
@aluanhaddad I don't really see the point to see a discussion about a proposal which everybody find bad keeps going on but if it's the whish of the community, I accept, let's reopen this. |
Beta Was this translation helpful? Give feedback.
-
@AdrienTorris The essential of this idea, namely reducing the repeating occurances of object names, pops up quite often. The need for this across other languages as well lead to the introduction of VB's So, this discussion is absolutely valid and vivid. Maybe some reader comes up one day with the solution to this for C#, maybe even a C# team member? :-) |
Beta Was this translation helpful? Give feedback.
-
@AdrienTorris: I think it'd be better if we take something like "With" (Please refer to VB.NET: https://docs.microsoft.com/en-us/dotnet/articles/visual-basic/language-reference/statements/with-end-with-statement). We should focus on HOW TO WRITE SIMPLE WHEN MEETING WITH MULTIPLE PROPERTIES FOR A CERTAIN OBJECT. We can import "with" (The first letter is a lower character) in C# (or create another new keyword such as "within"……ect) and we can say:
to
And sometimes, we should bind multiple events to the same function with the same delegate (in VB.NET we have "WithEvents"). C# should also have such a keyword to simplify our coding. |
Beta Was this translation helpful? Give feedback.
-
@MaleDong the Nevertheless, I had the exactly same idea, but haven't come up with an idea for a nice syntax yet
Maybe a combination like using with (Rectange r = new Rectange())
{
Width = 3,
Height = 5,
PrintTo(Console.Out)
} Question: shall Rectange r = new Rectange();
r...
{ Width = 3, [...] } (but that looks odd to me). |
Beta Was this translation helpful? Give feedback.
-
Why would I want to type: Rectangle r = new Rectangle();
within r
{
.Width = 3;
.Height = 5;
.Color = Color.Red;
} When a shorter form already exists? var r = new Rectangle
{
Width = 3,
Height = 5,
Color = Color.Red
}; |
Beta Was this translation helpful? Give feedback.
-
I agree with @DavidArno. Furthermore, the purpose of this issue is more about the update of an object than his instanciation. |
Beta Was this translation helpful? Give feedback.
-
Because you could not only initialize properties directly after the creation of the object, but also call methods on it. And you can do that later on mutable objects as well ( @DavidArno I know you loathe them ;-) And you mustn't take the examples too literally, they just want to clarify how the construct should be used. But to whom am I telling this... 😄 (PS: |
Beta Was this translation helpful? Give feedback.
-
@lachbaer:
What should I do if I wanna use "r" out of the "using with" block? The lifeline of "r" is only in the block of "using with"? This will make others puzzled. Based on the two reasons, I have to say that I don't think using with……a good idea, though it looks nicer:( An easy declaration of a veraible by multiple assignments is just to mock what we do in VB.NET. That's enough, clear and simple. If you don't wanna import "with" into C#. Another idea is just using something like this:
The syntax for multi-assignments is: declaration of a variable (e.g: a); The new operator ".=" just means "multi-prop assignments". After it we must use a block by “{}”, with properties there, seperated by a comma. What's more——I don't suggest "Function-calling" in the new syntax created by me. Because this is PURELY for prop-assignments. What do you think of this? |
Beta Was this translation helpful? Give feedback.
-
Similar discussion : #1887 |
Beta Was this translation helpful? Give feedback.
-
@DavidArno Actually, I have come to hate object initializers over time due to the fact that when one piece fails, it's hard to know which one. I would say that this is due to a miss-use of the feature but, but I have seen such problems enough times by now for me to avoid it all together... Note though, I would certainly not say that it's an argument for the suggestion in this issue or for any of the with/within constructs... Just wanted to throw a negative view in on the supposed object initializer "better syntax" you outlined. |
Beta Was this translation helpful? Give feedback.
-
@jeme The debugger team could enable substatement-level stepping, and that would enable the syntax to be used on its own merits rather than with a tooling side effect. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Sometimes, when I update the value of a variable using a method, I forget to set the equal, and it can be a bug hard to find because the code looks good.
The idea here is to allow to update the value of a variable using a methods with a syntax near than this one :
And could result code blocks like :
Or :
Beta Was this translation helpful? Give feedback.
All reactions