In the code below I get the correct type of the property (PropertyA
) when I get it straight from the hashmap.
When I proxy this call through the get method in ClassAbstract
the type is PropertyAbstract[_ <: A]
Is there a way to proxy the call to the hashmap and keep the correct type?
Another question is how can I add objects to the revs
array with type checking?
class A
class B extends A
class C extends A
abstract class PropertyAbstract[T] {
val revs = new java.util.ArrayList[T]
}
class PropertyA extends PropertyAbstract[B]
class PropertyB extends PropertyAbstract[C]
abstract class ClassAbstract {
val props: scala.collection.immutable.HashMap[String, PropertyAbstract[_ <: A]]
def get(prop: String) = props.get(prop).get
}
class Class extends ClassAbstract {
val props = collection.immutable.HashMap(
"prop1" -> new PropertyA,
"prop2" -> new PropertyB
)
}
object Test extends App {
val the_class = new Class
val proxied_prop = the_class.get("prop1")
val direct_prop = the_class.props.get("prop1").get
// wont compile (found: B required: _$1 <: A)
proxied_prop.revs.add(new B)
// wont compile (found: B required: C with B)
direct_prop.revs.add(new B)
}
The wanted result is that I could add an element of type B
to prop1, but not an element of type C