Skip to content

Instantly share code, notes, and snippets.

@olivier-spinelli
Created February 11, 2022 11:04
Show Gist options
  • Save olivier-spinelli/43087e4db9d379fe7936935a23266d98 to your computer and use it in GitHub Desktop.
Save olivier-spinelli/43087e4db9d379fe7936935a23266d98 to your computer and use it in GitHub Desktop.
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