console

How to start a Scala REPL session inside SBT (Simple Build Tool)

Scala FAQ: How do I start a Scala REPL session from SBT (i.e., inside the Simple Build Tool)?

Answer: Use console or consoleQuick:

  • 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.

The console command process looks like this:

> sbt
[info] Loading project definition from /Users/al/Projects/Cats101/project
[info] Set current project to Cats101 (in build file:/Users/al/Projects/Cats101/)

> console
[info] Starting scala interpreter...
[info]
Welcome to Scala 2.12.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_131).
Type in expressions for evaluation. Or try :help.

scala> _

If you wanted to see how to start a Scala REPL session from inside SBT, I hope this helps.

How to use ScalaCheck in the SBT console

If you add ScalaCheck to an SBT project like this:

libraryDependencies += "org.scalacheck" %% "scalacheck" % "1.13.4" % "test"

it’s only available in the SBT “test” scope. This means that when you start a Scala REPL session inside of SBT with its console command, the ScalaCheck library won’t be available in that scope.

To use ScalaCheck with the SBT console (REPL), don’t use its console command — use test:console instead. A complete example looks like this:

$ sbt

> test:console

scala> import org.scalacheck.Gen.choose

Note that after you type test:console your project may be compiled, so that step may take a few moments.

In summary, use SBT’s console command to start a “normal” Scala REPL inside SBT, and use test:console to start a REPL that you can run tests inside of. (Note that this same advice also applies to using ScalaTest or specs2.)

The Chrome JavaScript debug console and [Object object]

As I’ve gotten back into client-side web development -- specifically using Sencha Touch 2, a JavaScript framework -- I’ve quickly run into a problem in the Chrome JavaScript debug console. When I try to use console.debug() to debug an object, I get this non-useful output about the JavaScript object I’m printing:

[Object object]

There may be better ways to print the desired object, but I quickly discovered this trick to printing all of the properties of a JavaScript object:

Getting help on Play Framework console commands

It took me a few minutes to figure out that when you want help on Play Framework console commands (i.e., the Play command line tool), you may get more useful help by typing help play instead of just help. Using the first command shows information on commands like clean, compile, console, etc., which is what I'm more interested in.

To take a quick look at this, here's what you get when you type help play:

Common SBT commands (Scala SBT)

I had the common Scala SBT commands in the notes for my project named SARAH, and thought I'd list them here to make them easier to find:

Scala shell scripts and the command line: Prompting the user, and reading input

A great thing about Scala is that not only is it scalable, it was also created to help you work on small tasks, including being useful in shell scripts. This includes small shell script tasks like prompting a user interactively from a shell script, and reading their input.

You can prompt users with print commands like println and print, and you can read their input with all of these methods that are available on the Scala Console class:

Mac OS X debug tip - Check the log files

Mac OS X debug FAQ: Help, I'm having a problem with [fill in the blank] on my Mac OS X system, how do I troubleshoot it?

I just ran into a problem when trying to create a Safari web clip dashboard widget, where the web clip isn't properly updating itself, and I stopped for a moment to think about how to troubleshoot/debug this Mac OS X problem. That's when I thought, "Okay, a Mac is a Unix system, check the log files." So I cracked open a Mac Terminal window, and checked the system log file: