I have this below Class hierarchy in Java
Class Container
{
private List<? extends Element> element;
public List<? extends Element> getElements()
{
return elements;
}
public void setElements(List<? extends Element> elements)
{
this.elements = elements;
}
}
MyElement extends Element
{
int a;
}
In my scala services, I use these classes as below -
var elements: java.util.List[_ <: Element] = container getElements
elements match {
case null => elements = new ArrayList[MyElement]();
case _ => ;
}
//additional service code here,
//and then try to create an object and add it to the existing list of elements as below.
val element = new MyElement
//..... other prog logic..
elements.add(element)
I get a compilation error when the element is added to the list
I see that the error message is listed as -
type mismatch; found : element.type (with underlying type com.vo.MyElement) required: _$7 where type _$7 <: com.vo.Element
Is there any way to fix this issue? Why is Scala compiler not able to decipher that MyElement is indeed a subclass of Element and can be added to the main list.
Really hope someone helps me out. I have tried several alternatives and I just can't get this working. Am I missing something or am I really stupid?
Thanks, Adarsh