Proposal: restrictions for get accessor for interfaces and abstract classes #8356
Unanswered
iPilot
asked this question in
Language Ideas
Replies: 3 comments 5 replies
-
If you need a discriminator, look at JsonSubTypes |
Beta Was this translation helpful? Give feedback.
0 replies
-
I think should be part of the overall |
Beta Was this translation helpful? Give feedback.
1 reply
-
I'm not sure I understand the purpose. As a contract invoked via virtual call whether or not the member is implemented with a const expression doesn't really matter, it's not like the compiler or runtime can inline or fold the invocation. As for the discriminator use case, that is something much better served through attribute metadata as that can be read via reflection. The method body isn't, regardless of whether or not that body is a const expression. |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Description
Additional constraint for property
get
accessor:expr
property accessor:Updated: impossible to implement - discarded
const expr
property accessor:expr
and to be evaluated only using constants expressions, including otherconst expr
properties what makes the property accessor aligned with existing constants behaviorUpdated: simply
const
literal could be used for that purposeExamples
Intended usage
Initially idea grows from JSON polymorphic serialization / deserialization. Currently,
JsonPolymorphicAttribute
requires for each type metadata be passed toJsonDerivedTypeAttribute
attribute. That metadata access requires discriminator value to be declared either as a constant field in derived class or somehow else. Additionally, current implementation behaves very weird when combination of custom naming policy,Enum
as discriminator and custom converter are used.const expr
would allow to add a compile-time validation for a such polymorphic hierarchy as the base class would demand explicitly implement property used as polymorphic discriminator. Also, such properties could be attributed with custom JSON converter.I'm not sure, but maybe Native AOT compilation could benefit from this way of metadata delivery.
Beta Was this translation helpful? Give feedback.
All reactions