[Proposal]: Introduction of the =.
Operator for In-Place Method Calls with Assignment
#8303
Replies: 3 comments 3 replies
-
I don't understand what it does. Is it a shorthand for this? // Current
string a = "hey";
a = a.Split(...);
// Proposed
string a = "hey";
a .=. Split(...); That seems like a lot of effort for incredibly little gain. |
Beta Was this translation helpful? Give feedback.
-
I don't like this because:
I'm not saying you should stop using these methods full stop, but given these concerns, I'm not sure it's a good idea to make a language feature which simplifies this pattern any further. |
Beta Was this translation helpful? Give feedback.
-
As an alternative we could allow "ref" and "this" to be used as the first parameter on an extension method by the compiler (like VB) so that at least we have a cleaner workaround for this. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Rationale:
The primary rationale for introducing the
=.
operator is to reduce errors, code size, and typing effort when performing in-place operations on variables. This operator aims to provide a streamlined and intuitive syntax for invoking methods that modify the variable directly, enhancing code readability and maintainability.Proposal:
New Syntax:
myvar=.someMethod(parameters); //assigns myVar to the return of someMethod
This syntax reduces the need for repeated references to the variable, minimizing the potential for errors and making the code more concise.
Syntax Definition and Example:
Syntax Definition:
Example:
Simplete and cleaner than alternative:
myDescriptiveVaribaleName = myDescriptiveVaribaleName.Replace("this", "that");
Compatibility and Conflicts:
The proposed
.=
operator should not conflict with existing C# syntax. Here are the key points regarding compatibility:=.
operator does not interfere with existing operators or syntax. It introduces a new operation distinct from current member access and method calls.=.
syntax. This change involves ensuring the parser can distinguish between=.
and other dot-based syntax elements.=.
modifies the left-hand variable directly, similar to how+=
and-=
modify the left-hand variable by applying an operation.Implementation Details:
=.
operator.Similarity to
+=
and-=
Operators:The
+=
and-=
operators in C# are compound assignment operators that perform an operation and assignment in one step. They work by:The
=.
operator can follow a similar model:Date: 2024-07-19
Beta Was this translation helpful? Give feedback.
All reactions