在 scala actor 中传递集合
当我需要将类型化集合传递给参与者时,我在 react
方法中收到“未检查”警告:
val actor = actor {
loop {
react {
case a:List[String] => // do something
}
}
}
如何解决此问题?我尝试在一个单独的类中对集合进行拳击(但这很丑陋且麻烦),并且在之后仅转换集合(case a:List[_] => a.asInstanceOf[List[String]]
)由 actor 接收它不是类型安全且危险的。
When I need to pass typed collections to an actor, I get an "unchecked" warning in my react
method:
val actor = actor {
loop {
react {
case a:List[String] => // do something
}
}
}
How can I work around this? I tried boxing collection in a separate class (but that is ugly and cumbersome), and just casting collection (case a:List[_] => a.asInstanceOf[List[String]]
) after receiving it by actor is not type-safe and dangerous.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于 JVM 不会跟踪泛型的类型,因此您无法知道
List[_]
是List[String]
,除非您检查每个元素并检查它是否是一个字符串。事实上,最好的选择是将集合放入一个单独的类中。事情不必那么糟糕!Because the JVM does not keep track of the type of generics, you can't know that a
List[_]
is aList[String]
unless you examine every element and check that it is a string. Your best bet is in fact to box the collection in a separate class. It need not be so bad!