repl

Scala REPL - java.lang.OutOfMemoryError: Java heap space error

I just got a “java.lang.OutOfMemoryError: Java heap space error” when trying to use the Scala REPL to analyze a large XML dataset:

Scala REPL - There were deprecation warnings, re-run with -deprecation

Whenever I used to get the Scala REPL warning message, "There were deprecation warnings, re-run with -deprecation", I used to have to restart the REPL with the -deprecation flag to see the warning message, like this:

$ scala -deprecation

That works, but sadly, you had to lose your entire REPL session to run it.

Use :warnings

Thanks to the Scala gods, this is no longer necessary in Scala 2.10 (and newer). Now, when you get the REPL deprecation warning message, like this:

A short video introduction to the Scala REPL and variables

I didn't feel like going out in the rain and snow yesterday here in the Boulder, Colorado area, so I decided to stay indoors and create a video introduction to the Scala REPL and the Scala variable types, var and val. Without any further ado, here's the video:

Scala REPL "She's gone rogue" error message

In a little bit of accidental Scala REPL fun this afternoon, I was testing a recursive algorithm like the following simple factorial algorithm, and forgot a return statement:

def factorial(n: Int):Int = {
  if (n == 1) n    // forgot 'return' here
  factorial(n - 1)
}

When I pasted that code into the Scala REPL and then tried to test it as shown below, it didn't come back for a while, until it failed with this error message:

How to enter multiline commands (statements) into the Scala REPL

When you want to test a multiline command/statement in the Scala REPL, you can easily run into a problem where the REPL gets confused, or more accurately, it attempts to evaluate your statement too early.

As a simple example of this, imagine that you want to test the Scala "if" statement syntax. You begin typing your if statement normally, but when you hit [Enter] after your second line, you'll see that everything blows up:

Scala REPL - How to show more methods on a class/object in the REPL

When you're working in the Scala REPL and want to see what methods are available on a class/object, you can create an instance of an object, follow that with the "." character, and then press the [Tab] key. This process, known as "tab completion" in the REPL, gives you a preliminary list of methods that can be called on the object.

Here's what this looks like when we try it on an Int object:

How to show Scala String and StringOps methods in the REPL

Just a quick note here today on how to show methods from the String class in the Scala REPL, as well as methods from the StringOps class, which aren't seen as easily.

First, if you've used tab completion in the REPL, you may already know that you can show many String class methods in the REPL. If you hit the [Tab] key once you'll see a short list of String methods:

How to add a new jar file to the Scala REPL classpath (interactive command line)

Scala REPL FAQ: How do I add a Jar file to the Scala REPL classpath? (The Scala REPL is the interactive command line you get if you just type "scala" at your command line.)

To add a new jar file to the Scala REPL classpath (interactive command line classpath), use the ":cp" command at the command line, like this:

scala> :cp myjar.jar

After you do this, you should see a reply from the REPL like this:

Syndicate content