0

This is what I've written in a Xtend class:

    def getEntityList(String indct, String criterion) {
    var entities
    Switch(indct){
    case "PAR": entities = obj.getParamList.filter[definition.contains(criterion)]
    case "REF": entities = obj.getRefList.filter[definition.contains(criterion)]
    default: entities = null
    }
return entities
}

As in the above code, entities is a raw list type the initialization of which I'm trying to do based on a condition. Depending on the condition the entities list will either have the parameters or the references. I think this is not straight forward like in Perl as Xtend is a statically-typed language.

How do I achieve the above in Xtend 2?

Sujju
  • 97
  • 8

1 Answers1

0
var entities = switch(indct) {
  case 'PAR': obj.getParamList.filter[definition.contains(criterion)]
  case 'REF': obj.getRefList.filter[definition.contains(criterion)]
}

entities will now have the type List<? extends "common super type of param and ref">. Is that what you were asking for?

Sebastian Zarnekow
  • 6,609
  • 20
  • 23
  • Thanks. Tried the switch as a lambda which gives an additional `Object` type wrapping to it. This is better. Still, when I need to access the elements of `entities` and apply specific operations to them, I'm unable to do so as it's taken as the super type. For ex. `entities.filter[p|p.paramDefinition]` gives me a compile-time error. How do I cast it to the specific subtype depending on the value of indct? – Sujju Jun 03 '15 at 09:12
  • 1
    That would be entities.filter(Param).filter[ p | p.paramDefinition] I presume. – Sebastian Zarnekow Jun 03 '15 at 14:03
  • Thanks. How did I not think about that? Should start drinking coffee maybe. – Sujju Jun 05 '15 at 09:38