Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to implement cast operator to a nullable type. #29749

Closed
stackh34p opened this issue Sep 10, 2018 · 1 comment
Closed

Unable to implement cast operator to a nullable type. #29749

stackh34p opened this issue Sep 10, 2018 · 1 comment

Comments

@stackh34p
Copy link

Version Used:
.NET Sdk v2.1.400 (as shown by dotnet --version)
IDE: Visual Studio Community 2017 v15.8.1
OS: Windows 8.1

Steps to Reproduce:

  1. Create a custom struct type (example in C#) -- for instance public struct MyStruct { }

  2. Overload cast operator from Nullable<MyStruct> to MyStruct:

    public static implicit operator MyStruct(Nullable<MyStruct> nv) => nv.GetValueOrDefault();

  3. Compile the project.

Expected Behavior:
The project compiles successfully and user code should be able to perform implicit casts from MyStruct? values to MyStruct ones. For example, consider the following code:

public class Foo
{
    public void Method(MyStruct myStruct) { }
}

Nullable<MyStruct> nullMyStruct = null;
Nullable<MyStruct> notNullMyStruct = new MyStruct();

var foo = new Foo();

In the below snippet, due to the implicit cast, foo.Method should receive an equivalent of new MyStruct()

foo.Method(nullMyStruct); 

And similarly, here the implicit cast should pass to foo.Method what is stored in notNullMyStruct.Value

foo.Method(notNullMyStruct);

Actual Behavior:
A compilation error occurs:

error CS0555: User-defined operator cannot take an object of the enclosing type and convert to an object of the enclosing type

The compiler performs some type of erasure preventing it from distinguishing the Nullable<MyStruct> type from the MyStruct one.

Personal note

The particular use case for such a cast operator is illustrated in a question I asked in Stackoverflow a couple of days ago. From there I was advised to raise the current issue.

I understand the importance of the Nullable<T> type not permitting implicit casts to T which is part of the design of this type. However, the documentation doesn't seem to explicitly specify if the user code should be able to overcome this limitation, by defining a custom cast operator, or not (due to violating the design). The compiler error most certainly does not help finding out.

@jaredpar
Copy link
Member

This behavior is "By Design" for C#. There is no way to customize the process of converting between a struct and it's nullable wrapper. If you want to suggest a behavior change please open this on the charplang repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants