Created
February 11, 2022 11:04
-
-
Save olivier-spinelli/43087e4db9d379fe7936935a23266d98 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
// notnull constraint cannot handle both in and Nullable<T>. | |
public interface IWriter<T> where T : notnull | |
{ | |
void Write( in T o ); | |
void WriteNullable( in T? o ); | |
} | |
public class ValueTypeWriter<T> : IWriter<T> where T : struct | |
{ | |
public void Write( in T o ) | |
{ | |
} | |
public void WriteNullable( in T? o ) | |
{ | |
} | |
} | |
// It's worse than that: notnull constraint cannot be "specialized" to handle nullable value types. | |
public interface INullableWriter<T> where T : notnull | |
{ | |
void WriteNullable( T? o ); | |
} | |
public class NullableValueTypeWriter<T> : INullableWriter<T> where T : struct | |
{ | |
public void WriteNullable( T? o ) | |
{ | |
} | |
} | |
public class C { | |
public void M() | |
{ | |
IWriter<int> w = new ValueTypeWriter<int>(); | |
w.Write( 8 ); | |
int? n = default; | |
w.WriteNullable( in n ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment