Friday, December 28, 2012

Interactive Scala Console (REPL) for Play Application

Since I know Ruby/Rails so well, I constantly compare that technology stack with Scala/Play.

Ruby has IRB (interactive ruby shell).  Scala has REPL.

When developing a Rails application, I frequently use the Rails Console to debug Rails project code.  Execute the following command from the OS shell, e.g., Bash, in your Rails project:

rails c

The default environment is development, but you can also open the rails console for test or production (or any custom environment you create)

Rake is the Ruby-based build tool for Rails apps.

Similarly, SBT is the scala-based build tool used for Play applications.

SBT is typically used to compile and execute a Scala scripts containing dependency declarations or other sbt settings.

I created a script to start SBT for my project that looks like:

java -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000 -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=512m -Xmx2048M -noverify -jar $HOME/opt/sbt/bin/sbt-launch.jar "$@"

That allows me to run commands like:

project project_name

But I wanted to actually run scala commands and exercise application APIs.

I'd get errors like the following:

SBT Console

Listening for transport dt_socket at address: 8000
[info] Loading project definition from /hs/dev/company/project
[info] Set current project to ap (in build file:/hs/dev/company/)
project:master:5.0-SNAPSHOT> var allProducts = new ListBuffer[String]()
[error] Not a valid key: var (similar: start, run, target)
[error] var allProducts = new ListBuffer[String]()
[error]    ^
project:master:5.0-SNAPSHOT> import collection.mutable.ListBuffer
[error] Not a valid key: import (similar: port)
[error] import collection.mutable.ListBuffer
[error]       ^

... until I learned how to startup the Scala REPL, defining the dependencies that should be on the classpath, and adding that to my SBT script:

java -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000 -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=512m -Xmx2048M -noverify -jar -Dnet.tstllc.common.util.devmode=yes  -Dsbt.main.class=sbt.ConsoleMain $HOME/opt/sbt/bin/sbt-launch.jar "$@"

SBT (with REPL) Console

$ sbt
Listening for transport dt_socket at address: 8000
[info] Set current project to default-3bc954 (in build file:/Volumes/HoneyBadger1TB/Users/lex/.sbt/boot/ivy-console/)
Welcome to Scala version (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_37).
Type in expressions to have them evaluated.
Type :help for more information.

scala> var allProducts = new ListBuffer[String]()
<console>:7: error: not found: type ListBuffer
       var allProducts = new ListBuffer[String]()

scala> import collection.mutable.ListBuffer
import collection.mutable.ListBuffer

scala> var allProducts = new ListBuffer[String]()
allProducts: scala.collection.mutable.ListBuffer[String] = ListBuffer()

scala> allProducts += "1"
res0: scala.collection.mutable.ListBuffer[String] = ListBuffer(1)

scala> allProducts += "2"
res1: scala.collection.mutable.ListBuffer[String] = ListBuffer(1, 2)



Special thanks to Kyle Unverferth!

Sponsor Ads


  1. Just found out that the above really only applies to non-play sbt projects.

    For play apps, similar to Rails apps, you can just type "console" at the sbt prompt :-)

  2. Great site for these post and i am seeing the most of contents have useful for my Carrier.Thanks to such a useful information.Any information are commands like to share him.
    PHP Training in Chennai