2

Is there a way I can implement something like an abstract class with a generic type? For example, I want to be able to write code like the following (I know there are issues there). The main error I get is, "type parameters not supported".

Ultimately I'd love to have a way to extend an abstract class and easily customize the concrete class to be specific to one particular Sobject type.

LeadAssignJob class

global class LeadAssignJob extends AbstractAsyncJob<Lead> {
    ...
    global override Boolean evaluate(List<Lead> records) { ... }
    ...
}

AbstractAsyncJob class

global abstract AbstractAsyncJob<T> {

    global abstract Boolean evaluate(List<T> records);

    global void process(List<T> newRecords, Map<Id, T> oldRecordsById) {
        ...
        if (evaluate(newRecords)) { ... }
        ...
    }
}

LeadTrigger

trigger LeadTrigger on Lead (after insert, after update) {
    new LeadAssignJob.process(Trigger.new, Trigger.oldMap);
}
1

1 Answer 1

2

They experimented with Parameterized Classes/Interfaces, but this feature was deprecated about four years ago. They've hinted at the idea that they might revive this feature at some point in the future, but we're not holding our breath. In the meantime, you'll have deal with just naming your classes and pseudo-parameterization:

public abstract class A {
  public abstract void evaluate(SObject[] records);
}

...

public class B extends A {
  void evaluate(Lead[] records) {
    ...
  }
  public override void evaluate(SObject[] records) {
    evaluate((Lead[])records);
  }
}

This is awfully annoying, but workable'ish. You might want to suggest an idea, and let me know so I can go vote, too.

2
  • Thanks sfdcfox... I found an existing idea, with just one vote from the poster, lol...
    – Marty C.
    Commented May 19, 2017 at 17:18
  • @MartyC. Huh. I know that I would use them like... all the time. They're so incredibly useful. I have probably a dozen things I'd go use them for immediately if they suddenly became available.
    – sfdcfox
    Commented May 19, 2017 at 17:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .