Skip to content

Instantly share code, notes, and snippets.

@aidan-harding
Created July 22, 2022 08:13
Show Gist options
  • Save aidan-harding/610261976a408a4186ae449245b9ffa1 to your computer and use it in GitHub Desktop.
Save aidan-harding/610261976a408a4186ae449245b9ffa1 to your computer and use it in GitHub Desktop.
Generics could mitigate the lack of covariant return types
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