Get function type from scala console

Starting to learn Scala, I would like to quickly see the method signature in the console. For example, in Haskell, I would do:

Prelude> :t map map :: (a -> b) -> [a] -> [b] 

This clearly shows the signature of the mapping function, i.e.:

  • A function that takes a and returns b
  • List

and returns

  • List b

which leads to the conclusion that map converts list a to list b, applying a function to each element of the list.

Is there a way to get a method type in a similar way in Scala?

UPDATE:

Attempted response by Federico Dahl Maso and getting this

 scala> :type Array.fill <console>:8: error: ambiguous reference to overloaded definition, both method fill in object Array of type [T](n1: Int, n2: Int, n3: Int, n4: Int, n5: Int)(elem: => T)(implicit evidence$13: scala.reflect.ClassManifest[T])Array[Array[Array[Array[Array[T]]]]] and method fill in object Array of type [T](n1: Int, n2: Int, n3: Int, n4: Int)(elem: => T)(implicit evidence$12: scala.reflect.ClassManifest[T])Array[Array[Array[Array[T]]]] match expected type ? Array.fill 

Obviously, the padding method is overloaded and: type cannot decide which overload to display. So, is there a way to display the types of all method overloads?

+4
source share
3 answers
 scala> :type <expr> 

displays the type of expression without evaluating it

+4
source

Scalas REPL can only show types of valid expressions, here it is not as powerful as ghci. Instead, you can use scalex.org (the equivalent of Scalas Hoogle). Enter array fill and get:

 Array fill[T]: (n: Int)(elem: β‡’ T)(implicit arg0: ClassManifest[T]): Array[T] 
+2
source

:type <expr>

works, but if expr is a method, you need to add an underscore to treat it as a partially applicable function.

 scala> def x(op: Int => Double): List[Double] = ??? x: (op: Int => Double)List[Double] scala> :type x <console>:15: error: missing arguments for method x; follow this method with `_' if you want to treat it as a partially applied function x ^ scala> :type x _ (Int => Double) => List[Double] 
0
source

Source: https://habr.com/ru/post/1436539/


All Articles