-
Notifications
You must be signed in to change notification settings - Fork 20
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
Implement IFromDiscriminator interface (.NET 6+) #232
Comments
We've considered that, a couple of challenges with that approach though:
Besides standardized/cleaner code, what other benefits do you think that would yield? |
"only available for dotnet from a design perspective" "would require us to drop netfx and netcore < 6 compatibility" public class SomeModel: IParsable
#if NET6_0_OR_GREATER
, IFromDiscriminator<SomeModel>
#endif About benefits - well, isn't standardized/cleaner code enough of a benefit? |
Sure, but it has a cost in terms of binary size at the end. And if it doesn't enable new scenarios, we're unlikely to accept such a change. |
Ah, that is just a sample interface I've crafted for demonstration (though it can be useful as it is). Sorry for not making it clearer. |
I'm still trying to understand the design here (outside of the partial discussion on the other thread).
I'm not sure this would be a better experience than what we have today? (besides the fact that you don't need to get the parse node yourself) |
The trick is with So, it's static, not instance, method provided through the interface. Does it explain it? |
ah, I missed that detail, thanks for pointing this out. What if we had instead public interface IFromDiscriminator<T>
where T: IParsable
{
#if NET7_OR_GREATER
static abstract T CreateFromDiscriminatorValue(IParseNode parseNode);
#endif
} (note the removal of IAdditionalDataHolder as well) This way we could add the interface declaration to all models, and it'd work in any context? Technically we could even have a default implementation for all classes that don't have discriminated types like this, and omit generating generate the method with conditional directive, leading to smaller binary size for more modern applications. public interface IFromDiscriminator<T>
where T: IParsable, new()
{
#if NET7_OR_GREATER
static virtual T CreateFromDiscriminatorValue(IParseNode parseNode)
{
_ = parseNode ?? throw new ArgumentNullException(nameof(parseNode));
return new T();
}
#endif
} Which would lead to this in the models + #if !NET7_OR_GREATER
- public static new User CreateFromDiscriminatorValue(IParseNode parseNode)
+ public static override User CreateFromDiscriminatorValue(IParseNode parseNode)
{
_ = parseNode ?? throw new ArgumentNullException(nameof(parseNode));
return new User();
}
+ #endif Thoughts? |
First solution - fine with me. |
I'm a bit confused by this statement, where the second sentence seems to contradict the first one? |
I meant that if you implement it like that, there would be no code reduction (assembly size would still be the same or a few bytes larger) though other types will gain the functionality. OTOH all other supported types gain CreateFromDiscriminatorValue though interface, so there is code reduction in that sense. Makes sense? |
Thanks for clarifying! I'd understand that the method prototype still gets declared in every implementer (IL-wise), but don't you think we'll save at least the method body? (declared once instead of on every type) |
This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. |
Models could implement an interface like this for .NET 6+:
Then such methods are possible:
The text was updated successfully, but these errors were encountered: