Created
July 22, 2022 08:13
-
-
Save aidan-harding/610261976a408a4186ae449245b9ffa1 to your computer and use it in GitHub Desktop.
Generics could mitigate the lack of covariant return types
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CovariantReturnTypes { | |
virtual class X { | |
virtual Object foo() { return 0; } | |
} | |
// I want to do this, but the compiler says "Method return types clash: String vs Object", see | |
// https://ideas.salesforce.com/s/idea/a0B8W00000GdXOCUA3/support-covariant-return-types-in-apex | |
class Y extends X { | |
override String foo() { return ''; } | |
} | |
// I end up changing the method name | |
class YT extends X { | |
String fooT() { return '';} | |
} | |
// If I had generics, the base class would be generic | |
virtual class XG<T> { | |
virtual T foo() { return null; } | |
} | |
class YG extends XG<String> { | |
override String foo() { return 'YG'; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment