Search code examples
xtend

How to initialize a variable dynamically in Xtend 2


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?


Solution

  • 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?