[API Proposal] Enhance support for overriding default(T) where T: class #8298
Unanswered
Sophisticated-IS
asked this question in
Language Ideas
Replies: 2 comments
-
Many codes rely on the fact which interface IHasDefaultValue<T>
{
virtual static T? GetDefault() => null;
}
class Foo : IHasDefaultValue<Foo>
{
static Foo? IHasDefaultValue<Foo>.GetDefault() => new Foo();
}
T? DefaultOf<T>() where T : IHasDefaultValue<T> => T.GetDefault();
var v = DefaultOf<Foo>(); |
Beta Was this translation helpful? Give feedback.
0 replies
-
The same issue also applies to value types, especially when the type is a wrapper of some reference type. Ideally, we should track |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Motivation
Now default(T) is completely unusable for reference types because it always compiles to null. And you anyway have to use static fields to have default value different from null.
If we could override this behavior and encapsulate "Default value" of Type in class it would greatly increase default(T) usability.
Proposing Behavior:
IF class not implements IDefaultProvider than compile default(T) to null
IF implements then replace with method call to Default property
Example:
Beta Was this translation helpful? Give feedback.
All reactions