【问题标题】:Infer generic types on scala shell在 scala shell 上推断泛型类型
【发布时间】:2013-06-04 01:43:45
【问题描述】:

是否可以使用 scala shell 来推断泛型函数的类型?

我试图理解 Future.traverse (scala 2.10) 函数的类型。完整的泛型类型是

def traverse[A, B, M[_] <: TraversableOnce[_]](in: M[A])(fn: A => scala.concurrent.Future[B])(implicit cbf: generic.CanBuildFrom[M[A],B,M[B]], executor: ExecutionContext): Future[M[B]]   

这足够长,我几乎无法阅读,所以我认为尝试一些更具体的类型是个好主意。所以我尝试了:

> :t (x : List[_]) => (Future traverse x) _
List[_] => ((Any => scala.concurrent.Future[Nothing]) => scala.concurrent.Future[List[Nothing]])

这很有帮助,但如果我可以在 List 类型构造函数中指定类型会更好:

:t (x : List[a]) => (未来遍历 x) _

不幸的是(并且可以理解)给了我一个type a not found 错误。

有什么办法可以做到吗?

【问题讨论】:

    标签: scala type-inference interactive-shell


    【解决方案1】:

    也许

    scala> object Foo {
         | type A = Int
         | type B = Int
         | }
    defined object Foo
    
    scala> import Foo._
    import Foo._
    
    scala> :type  Future.traverse[A,B,List] _
    List[Foo.A] => ((Foo.A => scala.concurrent.Future[Foo.B]) => scala.concurrent.Future[List[Foo.B]])
    

    【讨论】:

    • 不错!正是我需要的。
    猜你喜欢
    • 2012-11-29
    • 2023-03-08
    • 1970-01-01
    • 2017-09-08
    • 1970-01-01
    • 1970-01-01
    • 2020-10-12
    • 1970-01-01
    • 2016-07-27
    相关资源
    最近更新 更多