Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a quick way to show the code of a method declared in the Scala Console?

I frequently use the Scala console to evaluate and test code before I actually write it down in my project. If I want to know the contents of a variable, I can just enter it and scala evaluates it. But is there also a way to show the code of methods I entered?

I know there's the UP-key to show single lines, but what I was searching for is to show the whole code at once.

like image 782
Björn Jacobs Avatar asked Jul 19 '12 13:07

Björn Jacobs


People also ask

What is the Scala REPL?

The Scala REPL (“Read-Evaluate-Print-Loop”) is a command-line interpreter that you use as a “playground” area to test your Scala code. To start a REPL session, just type scala at your operating system command line, and you'll see this: $ scala Welcome to Scala 2.13.0 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_131).

How do I get out of a shell in Scala?

To run your program, type run . To leave sbt shell, type exit or use Ctrl+D (Unix) or Ctrl+Z (Windows).

How do I open a Scala interpreter?

To run Scala from the command-line, download the binaries and unpack the archive. Start the Scala interpreter (aka the “REPL”) by launching scala from where it was unarchived. Start the Scala compiler by launching scalac from where it was unarchived.

How do I open a Scala shell in Linux?

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


1 Answers

There's a file in your home directory named .scala_history that contains all of your recent REPL history. I regularly copy and paste code from this file into project source files. It's not exactly the same as showing the code for individual methods in the REPL, but it might help you accomplish the same goals.

See the comments by Paul Phillips in this issue for a discussion of some related functionality in the REPL (grouping statements in the history):

At some point I implemented the logic for this, but the real obstacle is jline. It has enough trouble figuring out where the cursor is under the simplest conditions. Start throwing big multiline blocks into the history and it breaks down in tears. Would love to see this and SI-2547 addressed by the community.

...

I expect to fix this soon too, but it depends on how well the recent jline work goes. I implemented it long ago, and display issues are the impediment.

Both of these comments are over two years old, so I wouldn't hold your breath.

like image 170
Travis Brown Avatar answered Oct 16 '22 19:10

Travis Brown