Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Viewing the contents in <function1> from the scala repl

Tags:

scala

I can create anonymous functions in the scala repl like so:

scala> val a = (x: Int) => x * x
a: Int => Int = <function1>

But is there anyway of seeing what is inside after it has been created?

I'm thinking about situations where I would take a function and return a function. I'm just curious to see what the repl created as the return, rather than just the type of the returned value, so something like:

scala> val b = (f: (Int => Boolean)) => (x: Int) => ! (f(x))
b: (Int => Boolean) => (Int => Boolean) = <function1>

scala> val c = b((x: Int) => x % 2 == 0)
c: Int => Boolean = <function1>

I want to see what code has been generated inside c!

like image 751
Kat Avatar asked Aug 13 '15 09:08

Kat


People also ask

How do you get to scala REPL?

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

How do I use scala REPL?

Answer: Use the console or consoleQuick commands inside the sbt shell: Type console to start a REPL session from inside SBT. This (a) compiles your Scala project and then (b) starts a REPL session. Type consoleQuick if you don't want to compile your project before starting a REPL session inside of sbt.

What is Function1 in scala?

A function of 1 parameter. In the following example, the definition of succ is a shorthand for the anonymous class definition anonfun1: object Main extends App { val succ = (x: Int) => x + 1 val anonfun1 = new Function1[Int, Int] { def apply(x: Int): Int = x + 1 } assert(succ(0) == anonfun1(0)) }

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.


1 Answers

It's not as handy as same functionality in, say, clojure, since it shows compiled code but you can either get advantage of :javap:

scala> :javap -help
usage       :javap [opts] [path or class or -]...
-help       Prints this help message
-raw        Don't unmangle REPL names
-app        Show the DelayedInit body of Apps
-fun        Show anonfuns for class or Class#method
-verbose/-v Stack size, number of locals, method args
-private/-p Private classes and members
-package    Package-private classes and members
-protected  Protected classes and members
-public     Public classes and members
-l          Line and local variable tables
-c          Disassembled code
-s          Internal type signatures
-sysinfo    System info of class
-constants  Static final constants

scala> :javap -s a
Compiled from "<console>"
public class  {
  public static final  MODULE$;
    descriptor: L;
  public static {};
    descriptor: ()V

  public scala.Function1<java.lang.Object, java.lang.Object> a();
    descriptor: ()Lscala/Function1;

  public ();
    descriptor: ()V
}

Or instruct REPL to output code internals on compilation with scala -Xprint:typer (though it might be overly verbose, maybe someone can suggest less wordy compiler stage to use).

BTW, as you can see in :javap output, every REPL expression is implicitly wrapped with surrounding code, don't be confused -- scala does not perform it normally.

like image 144
om-nom-nom Avatar answered Oct 02 '22 00:10

om-nom-nom