Inverse operator for ?? #7855
Replies: 7 comments 52 replies
-
Sorry, but I'm really struggling to see where this would ever be useful; you want reassign a variable only if it's not null? Can you give a more concrete or real-world example of where this might be useful? |
Beta Was this translation helpful? Give feedback.
-
The specific syntax |
Beta Was this translation helpful? Give feedback.
-
I propose the following syntax:
How to memorize?If the left expression
Non-trivial examples for Case 1Buy a car only if we have a garage. Garage? garage = null;
Car? car = garage is null ? null: new Car(); // normal code
Car? car = garage ?> new Car(); // proposed alternate
class Car;
class Garage; Case 2
Fruits? fruit = null;
bool? isNut = fruit is null ? null : fruit == Fruits.Nut; // normal code
bool? isNut = fruit?.Equals(Fruits.Nut); // normal code
bool? isNut = fruit ?> fruit == Fruits.Nut; // proposed alternate
public enum Fruits
{
Nut,
Kiwi,
Melon
} Note: For my previous proposed syntax, see #7881 (comment) ContemplationFor the sake of fairness, let's consider the following:
|
Beta Was this translation helpful? Give feedback.
-
I'm against this idea because it seems like a request to support writing shorter code for the sake of writing shorter code, especially given your statement:
Emphasis mine. The use case
I do like the fact that you made a comment to another poster using the car and garage example, but it still left me asking the question "why are you creating a variable you don't need?". Everything I came up with around this ended in me refactoring it to something more legible than creating an unnecessary variable. For example: Car? car = await service.GetCar(id: 12345, cancellationToken);
if (car is null)
return;
Garage garage = new(car);
... |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
long story short, it would be nice to have an operator for this, but I don't feel strongly about it. if maintainers think we don't need it, it's fine with me, there are more important features that I am looking forward to like extions, unions, expression bodies and HKT (I know it's unlikely that we get HKT, but I am still hoping) |
Beta Was this translation helpful? Give feedback.
-
This "answer" is made only for ones supporting the proposal.
(*) : Feel free to ask for attribution removal. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I think this is a duplicate request, but..
If we have this code^
W q = w != null ? w : new W();
then we can shorten it in this code:
W q = w ?? new W();
But I often need to make logic that inversed, I need this:
Y q = w == null ? null : new Y();
It is very long expression and need to be shortened. Every time I hope in new version C# we will have something like
Y q = w !? new Y();
but when I try it in my current net8 project it give compile error.
Whar the problem to realize this function
Y q = w !? new Y();
?
Beta Was this translation helpful? Give feedback.
All reactions