Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't the ??-operator be applied to generic types, but a null-check can be applied?

Tags:

If I have a generic function that returns a T or some default value I want to use the ?? operator

T f<T>(T a, T b)
{
    return a ?? b;
}

This fails with the message

Operator '??' cannot be applied to operands of type 'T' and 'T'

I assume this is because T might not be nullable. But I would also assume that the ?? operator above should do the same as

T f<T>(T a, T b)
{
    var a_ = a;
    return a_ != null ? a_ : b;
}

And the second code compiles without a problem.

So why are those cases handled differently even though they should do the same?

like image 485
danielspaniol Avatar asked Aug 05 '19 11:08

danielspaniol


1 Answers

From this question on dotnet/csharplang:

The reason for this is that ?? is intended to unwrap the type on the left if the type on hte left is Nullable<T>. In other words, today i can write:

int? x ...;
int y ...;
int z = x ?? y;

So, it's not the case that "x ?? y" translates to "x != null ? x : y". It can translate to "x != null ? x.Value : y".

Since we don't know if T is a value or reference type, we cannot effectively do the transformation. However, when T is constrained to "class" or "struct" (and in the latter case, is a T?), then we can do the transformation.

See the rest of that thread for a wider discussion.

There's a discussion around relaxing this.

like image 122
canton7 Avatar answered Oct 02 '22 15:10

canton7