Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala REPL: How to find function type?

In Scala REPL one can find value types:

    scala> val x = 1
    x: Int = 1

    scala> :t x
    Int

Yet Scala REPL does not show the type information for functions:

    scala> def inc(x:Int) = x + 1
    inc: (x: Int)Int

scala> :t inc
<console>:9: error: missing arguments for method inc;
follow this method with `_' if you want to treat it as a partially applied function
       inc
       ^
<console>:9: error: missing arguments for method inc;
follow this method with `_' if you want to treat it as a partially applied function
          inc
          ^

How to find function type in Scala REPL ?

like image 303
Anton Ashanin Avatar asked Mar 25 '13 09:03

Anton Ashanin


People also ask

What is the type of a function in scala?

Scala functions are first-class values. Scala functions are first-class values. You must mention the return type of parameters while defining the function and the return type of a function is optional. If you don't specify the return type of a function, the default return type is Unit.

Does scala have a REPL?

The Scala REPL is a tool (scala) for evaluating expressions in Scala. The scala command will execute a source script by wrapping it in a template and then compiling and executing the resulting program.

What does the scala REPL stand for?

The Scala REPL (“Read-Evaluate-Print-Loop”) is a command-line interpreter that you use as a “playground” area to test your Scala code.

How do I get scala REPL from command prompt?

We can start Scala REPL by typing scala command in console/terminal.


1 Answers

Following the suggestion will work pretty well:

:t inc _
Int => Int

To give a bit more detail, the reason this is necessary is that Scala maintains a distinction between 'methods', which have native support in the JVM but which are not first class, and 'functions', which are treated as instances of FunctionX and seen as objects by the JVM. The use of the trailing underscore converts the former to the latter.

like image 68
Impredicative Avatar answered Sep 21 '22 17:09

Impredicative