我想要做的是定义条件的类,包括继承。如果有两个条件(或or-ed),则结果应该是更具体的类的条件,即查询条件。 所以,如果我有斯卡拉类型推理,并获得更具体的两种类型参数作为方法结果
class Parent
class Sub extends Parent
class SubSub extends Sub
一个Condition[Sub] and Condition[Parent] and Condition[SubSub]
应该产生一个Condition[SubSub]
。 条件也应该是不相容的,因为父母的条件也应该是子条件。因此,我定义(在AnyRef是一个额外的,外部约束):
class Condition[-A<:AnyRef] {
def and[R<:A, P>:R<:AnyRef](that:Condition[P]):Condition[R]=
new AndCondition[R](this, that)
}
class AndCondition[-A<:AnyRef](left:Condition[_>:A], right:Condition[_>:A])
extends Condition[A]
的AndCondition的打字似乎是确定的,因为这
val a:AndCondition[SubSub] =
new AndCondition(
new AndCondition(
new AndCondition(
new Condition[Parent],
new Condition[Sub]),
new Condition[Parent]),
new Condition[SubSub])
作品。我曾与和方法问题,中缀通话和上述定义,也可以工作上来:
val b: Condition[SubSub] =
new Condition[Parent] and
new Condition[SubSub] and
new Condition[Sub]
...样的,因为它停止和荷兰国际集团第四条件时工作:
val c: Condition[SubSub]=
new Condition[Parent] and
new Condition[SubSub] and
new Condition[Sub] and
new Condition[Parent]
结果在“无”推论:
error: inferred type arguments
[com.solvedirect.test.SubSub,com.solvedirect.test.SubSub,com.solvedirect.test.Parent]
do not conform to method and's type parameter bounds
[R <: Nothing,R <: R,P >: R <: AnyRef]
“减少”该“和链”由第一和第二两个条件支撑在一起(即(a和b)和(c和d)),使它再次编译。 我想我采取了一些错误的路线,但看不到它。对于不具体的问题标题,或者已经回答了这个问题,我很抱歉,但是我找不到它,可能也是由于缺乏描述性搜索条款。
谢谢你的时间。
问候,
梅西
我现在试着用相交结果类型定义'和'方法如下: 'def和[P <:AnyRef](that :条件[P]):条件[A与P] =新AndCondition [A与P](这,那)' 这似乎工作,但我不确定这是否具有意想不到的副作用。 – messi 2012-03-10 13:36:51