Proposal: non-null coalescing operator (meld operator?) #1972
Replies: 15 comments
-
For me personally, it is not difficult to understand, but I think it is rather hard to track down errors with this in large code bases. I personally also do not see an explicit need for this proposal, as it is rather a small-ish use case. I would rather see people replace this operator with a short-named, descriptive extension method. Also, what will happen, if people find more such use case? We will end up with a whole set of operators named |
Beta Was this translation helpful? Give feedback.
-
Man, I hate the ?. operator. I see it abused so much and it makes it hard IMO to understand what the control flow is. I saw this recently from someone I work with
What does that even do if the various things are null? I find this |
Beta Was this translation helpful? Give feedback.
-
The operator looks too much like Also I'm finding this very confusing. If It seems very niche, not useful, and confusing because I would expect The null coalasing operator is something used probably in every class, but yours wouldn't. It's also completely unreadable when you chain them. Why not just use an extension method: public static T Meld(this object obj, T rightOperand)
{
if (obj) return rightOperand;
else return null;
}
...
Foo(x.Meld(y)); It's only a few characters longer, but much more readable and doesnt involve needing a new feature that everyone else has to deal with. Not everything needs an operator. Also please reformat your question, your code is unreadable. |
Beta Was this translation helpful? Give feedback.
-
It's basically
It's kinda similar to #431, except less generic. |
Beta Was this translation helpful? Give feedback.
-
First, if The first lambda takes one arg The second lambda takes two args, This Frankly, the real confusion here stems from using poor names for things. what is an 'x'? Why would i expect an 'x' to have a ServerName? Same with I think there's a bunch wrong with that code. But the |
Beta Was this translation helpful? Give feedback.
-
This appears like a duplicate of #1676, except that that issue proposes |
Beta Was this translation helpful? Give feedback.
-
#1676 and #1661 (closed) are similar to this, it seems that there are several other attempts to find a solution to the problem "how do i avoid a function having to check its parameters for null all the time / deal with a function that can only accept non-null values when my thing might be null" #433 proposes to embed within the function parameters F(?value) Embedding some type of token '??' in function values leads to questions of 'are the parameters even evaluated' (explored in #444) which depend on if other parameters are null, i believe a prefix operator explicitly calling out things that are not null such as this or #1676 are most likely to receive acceptance |
Beta Was this translation helpful? Give feedback.
-
@orthoxerox Seems like the more appropriate shorthand operator would be int? myInt = myString >>= int.Parse; |
Beta Was this translation helpful? Give feedback.
-
If the pipe operator ever gets implemented then ?|> would be an obvious and intuitive operator. So int myInt = myString |> int.Parse;
\\becomes
int? myInt = myString ?|> int.Parse;`` |
Beta Was this translation helpful? Give feedback.
-
@bondsbw yes, except it's already right shift with assignment in c# |
Beta Was this translation helpful? Give feedback.
-
@orthoxerox Doh! Forgot about that. Too bad some of those operators were reserved back in version 1. Even back then in C++, the bit shift operators were more well known for stream manipulation. Should have been obvious that other uses would be preferable to bit shift. |
Beta Was this translation helpful? Give feedback.
-
C++ started that by hijacking the bitshifting ops, and I'm pretty sure the result of that is exactly why C# so strictly restricts how they can be overloaded. |
Beta Was this translation helpful? Give feedback.
-
Understood, but my point was that they probably shouldn't have been used for bit shift at all. It's a minor use case. Say those operators never existed in C#. Any new proposal to add them as bit shift operators into C# 8+ would probably not get off the ground. |
Beta Was this translation helpful? Give feedback.
-
(BTW I was mostly joking about the bind operator... C# really shouldn't try to become Haskell.) |
Beta Was this translation helpful? Give feedback.
-
😢 |
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.
-
Problem:
We have several other null coalescing operators to avoid using objects when they are null (?.) but don't have the capability to succinctly express a similar concern about function call parameters.
Proposal to simplify null handling:
Creation of a new binary operator !??
we have both a ?? operator for null handling, and a ! operator for inverting logic, so writing them together intuitively says "only do the following if not null"
The proposed operator x!??y can alternatively be expressed as (assuming that :
T? result (x is null)?null:y;
This wording was chosen to mirror the current null-coalescing operator:
This allows expressions such as strict parsing, with null handling:
int? myInt=myString!??Int.Parse(myString);
current equivalent code
Notes:
I believe with the introduction of null-tracking / provably not-null objects, an operator like this will simplify code in which only some code paths contain nullable objects, by allowing a simple form of "only call this function if these potentially nullable parameter are not null".
In order to implement this as an extension method, as mentioned by
The current implementation of 'Meld' function needs to be split into 2 types NullableMeld and ValueMeld, which either return TOut or Nullable, and would further need to have variants for different numbers of parameters
<edit: there are a few similar posts as noted below; however none of the others have gained traction>
Beta Was this translation helpful? Give feedback.
All reactions