我可以通过谷歌找到使用Typeable演员的所有例子都包含for表达式.这对我有用,但我想知道这是否合适.我对于无形的全新:这是我在lib中的第一次导入.
import shapeless.Typeable._
val blarg: Future[Any] = (worker ? ListOfLongsPlx(foo)) // I know Any === Try[List[Long]]
blarg.map {
_.cast[Try[List[Long]]] match {
case Some(Success(xs)) => xs
case Some(Failure(f)) => /* reporting the failure or just default: */ ; List()
case None => /*reporting bad cast just a default: */ List()
}
}
Run Code Online (Sandbox Code Playgroud)
我应该期待这种模式的问题吗?
要清楚,这是通过我的测试.
你的方法很好(虽然我不明白特定代码Try是如何工作的,因为不是Future),但你可以在模式匹配中实际获得一个稍微好一点的语法TypeCase:
import shapeless.TypeCase
import scala.util.{ Failure, Success, Try }
val thing: Any = Try(List(1L, 2L))
val TryListLongCase = TypeCase[Try[List[Long]]]
thing match {
case TryListLongCase(Success(xs)) => xs
case TryListLongCase(Failure(f)) => println(f); Nil
case _ => println("Some unexpected thing"); Nil
}
Run Code Online (Sandbox Code Playgroud)
结果将适当地键入为List[Long].
| 归档时间: |
|
| 查看次数: |
464 次 |
| 最近记录: |