【发布时间】:2019-08-05 09:13:59
【问题描述】:
我有类似这样的外部 API 设置:
abstract class PE {
type Event
abstract class C[R] {
def reply(r: R)
def persist[B <: Event](e: B)(callback: B => Unit): Persist
def fail(error: Throwable): Persist
}
abstract class Persist {
// ...
}
}
我想基于 scala 的 Try 类创建一个更像函数的包装器。我想出了以下代码:
def tryPersist[P <: PE, E <: P#Event, R](c: PE#C[R], tryEvent: Try[E])(replyProducer: E => R): PE#Persist = {
tryEvent match {
case Success(event) =>
c.persist(event)(_ => c.reply(replyProducer(event)))
case Failure(exception) =>
c.fail(exception)
}
}
但是,这不起作用,因为 scala 拒绝接受 PE#Persist 作为 PE.this.Persist。有什么遗漏吗?
inferred type arguments [E] do not conform to method persist's type parameter bounds [B <: _1.Event] c.persist(event)(_ => c.reply(replyProducer(event)))
在通话地点:
[error] found : MyPEImpl#Persist
[error] required: MyPEImpl.this.Persist
[error] persistEvent(ctx, command.eventFor(state))(_.playerBet)
PS:如果有人注意到,是的,它是 Lagom PersistentEntity 的 API
【问题讨论】:
标签: scala generics inner-classes