We have a Selenium WebElement we’re getting from the Java API but we’ve created a Scala class, Element, and mixin more specific traits (e.g. Clickable, Submittable, etc.).
Our method looks like:
toScalaElement(e : WebElement) = {
e.type match {
case Input => new Element(e) with Submittable
case Link => new Element(e) with Clickable
...
case _ => new Element
}
}
The return type is always Element as that’s the root class of all the cases. However, we would like to retain the mixin traits when it is returned.
It was advised to look at builders in Scala’s Collections API but we’re unsure how that relates to this particular application. Obviously, if there’s a better way than traits mixins that solution will be appreciated.
Update: I changed the case to match against sub-types instead of Strings, but essence of question remains unchanged.
I don’t think it’s doable. The method as a whole needs to have a single return type. In case of builders, this return type is generic and so can be different between method calls, but the compilers needs arguments to have different types to choose the builder. It looks like this:
And now you can get desired result here
but not here:
So if the static type of
eis justWebElement, builders don’t help.