假设我有
sealed trait AlphaNumericChar
sealed trait AlphaChar
case object A extends AlphaNumericChar with AlphaChar
case object B extends AlphaNumericChar with AlphaChar
sealed trait NumericChar
case object One extends AlphaNumericChar with NumericChar
case object Two extends AlphaNumericChar with NumericChar
这种结构允许我在AlphaNumericChar
上进行模式匹配并获得所有A,B,One,Two
,并在AlphaChar
和NumericChar
上进行类似的模式匹配,仅获得相关对象。
不允许我写:
def foo(x: AlphaNumericChar) = ???
def bar(x: AlphaChar) = foo(x)
即,仅针对某些类型的代理foo
调用。我当然可以写:
def baz(x: AlphaNumericChar with AlphaChar) = foo(x)
这可以工作,但这可能有点难看。
另一种方法是使AlphaChar
和NumericChar
扩展AlphaNumericChar
,但这将使我在AlphaNumericChar
上的模式匹配变得混乱,因为我现在必须处理_:AlphaChar
和_:NumericChar
以及我的case对象,这是不可取的。
是否有某种方式可以兼具两个世界中最好的一个?即。
AlphaChar
/ NumericChar
上的模式匹配项的详尽列表只有两个元素。AlphaNumericChar
上的模式匹配项的详尽列表只有两个元素。bar
语法的情况下使baz
函数正常工作。 答案 0 :(得分:0)
扩展AlphaNumericChar应该不是问题。像这样的东西工作正常:
public void fecharPainel(CloseEvent event) {
for (UIComponent pn : this.dashboard.getChildren()) {
if (pn.getId().equalsIgnoreCase(event.getComponent().getId())) {
this.dashboard.getChildren().remove(pn);
for (String widget : this.coluna1.getWidgets()) {
if (widget.equalsIgnoreCase(pn.getId())) {
this.coluna1.removeWidget(widget);
break;
}
}
for (String widget : this.coluna2.getWidgets()) {
if (widget.equalsIgnoreCase(pn.getId())) {
this.coluna2.removeWidget(widget);
break;
}
}
}
}
JsfUtil.warn("Painel removido com sucesso");
}
除非我误解了您想要什么?