Going further with discards "_" #9235
Replies: 7 comments 8 replies
-
Why not add overloads, and move common code to internal helper methods? |
Beta Was this translation helpful? Give feedback.
-
You are messing up with the nullability level in the example. You were requesting In current C#, it can be described with void Method(ref Result result)
{
if (!Unsafe.IsNullRef(ref result)) Compute result;
}
Method(ref Unsafe.NullRef(result)); or with void Method(out Result result)
{
Unsafe.SkipInit(out result);
if (!Unsafe.IsNullRef(ref result)) Compute result;
}
Method(out Unsafe.NullRef(result)); |
Beta Was this translation helpful? Give feedback.
-
This is all the point of my suggestion. Having a mechanism that tells the callee that the caller used a discard.
Presently, what I write is that:
```
void SVDDecomposition(double[,] a, bool sneeded, bool wneeded, bool vtneeded, out double[,]? s, out double[]? w, out double[,]? vt) …
```
There is a semantic and intrinsic matching between sneeded and the use of a discard in the call for out parameter s, and same from wneeded and w, vtneeded and vt.
I would appreciate if instead of writing the Boolean parameters and paying attention to set them to false when discard is used the compiler would take on charge itself.
It’s just cleaner, time saving, avoiding misused parameters, avoiding writing doc on these parameters, etc
|
Beta Was this translation helpful? Give feedback.
-
We can see that as [CallerMemberName] that gives a value to a parameter.
Roslyn is already able to recognize the _ as a discard symbol (see IDiscardSymbol for instance). Found in Microsoft C# documentation : “a discard communicates intent to the compiler and others that read your code”.
Since my suggestion is just at compile time, it may be possible, don’t you think ?
|
Beta Was this translation helpful? Give feedback.
-
I don't see much happening here unless the team changes their mind on exposing |
Beta Was this translation helpful? Give feedback.
-
Sir, I guess I have a workaround for you, that already works as of today: I agree with everyone else on the matter that "making discards distinguishable" is very complicated, you can't simply make the compiler pass But, you can exploit I'm not sure if there might also be spaces around the "_", but you can use |
Beta Was this translation helpful? Give feedback.
-
Ok for closing |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Discards have been introduced since a few C# versions. They prevent for naming a variable that won't be used later and may allow some compiler optimizations.
It would be very efficient if we can know that a discard is used.
Let's take, for instance, a method that computes the SVD decomposition of a matrix. It generally returns 3 matrices S, W and Vt. In some occasion, for instance to compute the rank of a matrix, only the W matrix is needed. Such a method may look like this:
and the call in the GetRank() method:
Basically, this call will compute the three matrices and two of them won't be used.
I'd like my SVDDecomposition to know that I don't need the matrices s and vt, without adding arguments myself.
For instance, with the use of a keyword inside the function:
The compiler will generate the code that gives the relevant value to needed(...), maybe by adding boolean parameters to the signature of the method.
Instead of needed, we could reuse the required keyword, not to extend too much the list of C# keywords:
For nullables, we could have a rule or an attribute like [NotNullWhenRequired].
The same should work for returning tuples:
Beta Was this translation helpful? Give feedback.
All reactions