【发布时间】:2017-07-06 22:59:49
【问题描述】:
我创建了Value Class
final class Feature(val value: Vector[Double]) extends AnyVal
致match 反对scala 中的那个类:
val d = new Feature(Vector(1.1))
s match {
case a:Feature => println(s"a:feature, $a")
case _ => println("_")
}
这可以正常工作,但在 Akka 中,具有上述相同的类,在 receive 方法中这是行不通的:
def receive = LoggingReceive {
case a:Feature =>
log.info("Got Feature: {}", a)
case _ => println("_")
}
当我执行代码时,虽然我发送的是Feature,但正在执行的case 语句是case _ => println("_"),但是,如果我将代码更改为:
def receive = LoggingReceive {
case a:Feature =>
log.info("Got Feature: {}", a)
case b:Vector[_] =>
log.info("GOT FEATURE")
case _ => println("_")
}
case b:Vector[_] 被执行。
Akka 文档提到:
推荐的实例化actor props 的方法是在运行时使用反射来确定正确的actor 构造 当所述构造函数采用以下参数时,不支持要调用的 tor 并且由于技术限制 价值类别。在这些情况下,您应该解压缩参数或通过调用构造函数创建道具 手动:
但不要提及匹配Value classes
更新
感谢 YuvalItzchakov 的帮助。 Actor的代码如下:
接收消息的Actor:
def receive = LoggingReceive {
case Feature(a) =>
log.info("Got feature {}", a)
// ....
}
发送消息的演员:
def receive = LoggingReceive {
// ..
case json: JValue =>
log.info("Getting json response, computing features...")
val features = Feature(FeatureExtractor.getFeatures(json))
log.debug(s"Features: $features")
featureListener.get ! features
// ..
}
【问题讨论】:
标签: scala akka value-class