Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does an implicit conversion operator from <T> to <U> accept <T?>?

This is a weird behaviour that I cannot make sense of. In my example I have a class Sample<T> and an implicit conversion operator from T to Sample<T>.

private class Sample<T> {    public readonly T Value;     public Sample(T value)    {       Value = value;    }     public static implicit operator Sample<T>(T value) => new Sample<T>(value); } 

The problem occurs when using a nullable value type for T such as int?.

{    int? a = 3;    Sample<int> sampleA = a; } 

Here is the key part:
In my opinion this should not compile because Sample<int> defines a conversion from int to Sample<int> but not from int? to Sample<int>. But it compiles and runs successfully! (By which I mean the conversion operator is invoked and 3 will be assigned to the readonly field.)

And it gets even worse. Here the conversion operator isn't invoked and sampleB will be set to null:

{    int? b = null;    Sample<int> sampleB = b; } 

A great answer would probably be split into two parts:

  1. Why does the code in the first snippet compile?
  2. Can I prevent the code from compiling in this scenario?
like image 762
Noel Widmer Avatar asked May 17 '18 09:05

Noel Widmer


People also ask

What is a implicit conversion?

An implicit conversion sequence is the sequence of conversions required to convert an argument in a function call to the type of the corresponding parameter in a function declaration. The compiler tries to determine an implicit conversion sequence for each argument.

What is an implicit operator in C#?

According to MSDN, an implicit keyword is used to declare an implicit user-defined type conversion operator. In other words, this gives the power to your C# class, which can accepts any reasonably convertible data type without type casting.

Is there an implicit conversion from dynamic to any type?

You can implicitly convert from an expression of type dynamic to any other type. What this means is that the compiler allows an implicit conversion (no cast operator) from the dynamic type to any other type and compilation succeeds.

Which operator can be used to do type conversions in C#?

Use the operator and implicit or explicit keywords to define an implicit or explicit conversion, respectively. The type that defines a conversion must be either a source type or a target type of that conversion. A conversion between two user-defined types can be defined in either of the two types.


2 Answers

You can take a look at how compiler lowers this code:

int? a = 3; Sample<int> sampleA = a; 

into this:

int? nullable = 3; int? nullable2 = nullable; Sample<int> sample = nullable2.HasValue ? ((Sample<int>)nullable2.GetValueOrDefault()) : null; 

Because Sample<int> is a class its instance can be assigned a null value and with such an implicit operator the underlying type of a nullable object can also be assigned. So assignments like these are valid:

int? a = 3; int? b = null; Sample<int> sampleA = a;  Sample<int> sampleB = b; 

If Sample<int> would be a struct, that of course would give an error.

EDIT: So why is this possible? I couldn't find it in spec because it's a deliberate spec violation and this is only kept for backwards compatibility. You can read about it in code:

DELIBERATE SPEC VIOLATION:
The native compiler allows for a "lifted" conversion even when the return type of the conversion not a non-nullable value type. For example, if we have a conversion from struct S to string, then a "lifted" conversion from S? to string is considered by the native compiler to exist, with the semantics of "s.HasValue ? (string)s.Value : (string)null". The Roslyn compiler perpetuates this error for the sake of backwards compatibility.

That's how this "error" is implemented in Roslyn:

Otherwise, if the return type of the conversion is a nullable value type, reference type or pointer type P, then we lower this as:

temp = operand temp.HasValue ? op_Whatever(temp.GetValueOrDefault()) : default(P) 

So according to spec for a given user-defined conversion operator T -> U there exists a lifted operator T? -> U? where T and U are non-nullable value types. However such logic is also implemented for a conversion operator where U is a reference type because of the above reason.

PART 2 How to prevent the code from compiling in this scenario? Well there is a way. You can define an additional implicit operator specifically for a nullable type and decorate it with an attribute Obsolete. That would require the type parameter T to be restricted to struct:

public class Sample<T> where T : struct {     ...      [Obsolete("Some error message", error: true)]     public static implicit operator Sample<T>(T? value) => throw new NotImplementedException(); } 

This operator will be chosen as a first conversion operator for nullable type because it's more specific.

If you can't make such a restriction you must define each operator for each value type separately (if you are really determined you can take advantage of reflection and generating code using templates):

[Obsolete("Some error message", error: true)] public static implicit operator Sample<T>(int? value) => throw new NotImplementedException(); 

That would give an error if referenced in any place in code:

Error CS0619 'Sample.implicit operator Sample(int?)' is obsolete: 'Some error message'

like image 155
arekzyla Avatar answered Sep 20 '22 15:09

arekzyla


I think it's lifted conversion operator in action. Specification says that:

Given a user-defined conversion operator that converts from a non-nullable value type S to a non-nullable value type T, a lifted conversion operator exists that converts from S? to T?. This lifted conversion operator performs an unwrapping from S? to S followed by the user-defined conversion from S to T followed by a wrapping from T to T?, except that a null valued S? converts directly to a null valued T?.

It looks like it's not applicable here, because while type S is value type here (int), type T is not value type (Sample class). However this issue in Roslyn repository states that it's actually a bug in specification. And Roslyn code documentation confirms this:

As mentioned above, here we diverge from the specification, in two ways. First, we only check for the lifted form if the normal form was inapplicable. Second, we are supposed to apply lifting semantics only if the conversion parameter and return types are both non-nullable value types.

In fact the native compiler determines whether to check for a lifted form on the basis of:

  • Is the type we are ultimately converting from a nullable value type?
  • Is the parameter type of the conversion a non-nullable value type?
  • Is the type we are ultimately converting to a nullable value type, pointer type, or reference type?

If the answer to all those questions is "yes" then we lift to nullable and see if the resulting operator is applicable.

If compiler would follow specification - it would produce a error in this case as you expect (and in some older versions it did), but now it does not.

So to summarize: I think compiler uses lifted form of your implicit operator, which should be impossible according to specification, but compiler diverges from specification here, because:

  • It is considered bug in specification, not in compiler.
  • Specification was already violated by old, pre-roslyn compiler, and it's good to maintain backwards compatibility.

As described in first quote describing how lifted operator works (with addition that we allow T to be reference type) - you may note it describes exactly what happens in your case. null valued S (int?) is assigned directly to T (Sample) without conversion operator, and non-null is unwrapped to int and run through your operator (wrapping to T? is obviously not needed if T is reference type).

like image 20
Evk Avatar answered Sep 18 '22 15:09

Evk