scala – 拦截/装饰PartialFunction
发布时间:2020-12-16 18:44:01 所属栏目:安全 来源:网络整理
导读:我如何拦截PartialFunction?例如在演员中,如果我想在将其传递到处理方法之前打印出以下接收方法中的所有内容: class MyActor extends Actor { def receive : Receive = process def process : Receive = { case Some(x) = /* do one thing */ () case None
我如何拦截PartialFunction?例如在演员中,如果我想在将其传递到处理方法之前打印出以下接收方法中的所有内容:
class MyActor extends Actor { def receive : Receive = process def process : Receive = { case Some(x) => /* do one thing */ () case None => /* do another thing */ () case _ => /* do something else */ () } } 解决方法PartialFunction 是您可以实施的特征.您不必使用case语法.
不幸的是,它并没有以你描述的方式编写方便的方法.最接近的是andThen方法,但是您传递的参数必须是常规函数,当在实际接收函数中未处理参数时,这可能导致匹配错误.所以你很难写它. class MessageInterceptor(receiver: Receive) extends Receive { def apply(msg: Any) = { /* do whatever things here */ receiver.apply(msg) } def isDefinedAt(msg: Any) = receiver.isDefinedAt(msg) } val process = new MessageInterceptor(receive) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |