在她的文章When OO and FP meet: returning the same type中,@ Jessitron给出了这个示例代码(略有不同):
object Policy {
def adjust[P <: Policy](p: P, d: Discount): P = {
case ap: AutoPolicy => new AutoPolicy
case lp: LifePolicy => new LifePolicy
// ... etc
}
}
应用模式匹配的值是多少?由于缺少match
关键字,我感到很困惑。
答案 0 :(得分:3)
如果没有def adjust[P <: Policy](p: P, d: Discount) = p match {
case ap: AutoPolicy => new AutoPolicy
case lp: LifePolicy => new LifePolicy
}
关键字,代码就无法编译:
PartialFunction
如果打算定义一个def adjust[P <: Policy](p: P, d: Discount): PartialFunction[P, _] = {
case ap: AutoPolicy => new AutoPolicy
case lp: LifePolicy => new LifePolicy
// ...
}
(似乎不太可能),它会有不同的签名:
{{1}}