Search code examples
scalatype-inferenceinteractive-shell

Infer generic types on scala shell


Is it possible to use the scala shell to infer the type of a generic function?

I was trying to undestand the type of the function Future.traverse (scala 2.10). The complete generic type is

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]]   

which is a long enough to be almost unreadable to me, So I thought it would be a good idea to try it is some more specific types. So I tried:

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

Which helped a lot, but it would be better if I could specify the type inside the List type constructor:

:t (x : List[a]) => (Future traverse x) _

Which unfortunately (and comprehensibly) gives me a type a not found error.

Is there any way to make this work?


Solution

  • Perhaps

    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]])