❔ Covariant Interface Return
Is there some reason why this doesn't work? I understand we can get this behavior with virtual methods and inheritance, but it feels like this should work with interfaces.
If FooBar is a Foo, this implementation should be valid no?
7 Replies
no, interfaces are contracts
any implementing classes must fulfill them exactly
It kinda does though
it doesn't
FooBar
is not Foo
So something like this works, we can override the return type and it still works because FooBar is a Foo, it still matches the contract of the original class but with more specificity
I'm not sure I'm understanding why they can't do the same things with an interface, if I cass FooBarGetter to IFooReturner, I'd still get a Foo from GetFoo
the feature is deliberately only implemented for classes
the implementation is much more complex for interfaces so they did not do it
Sad but that makes sense
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.