This page is no longer maintained — Please continue to the home page at www.scala-lang.org

A Taste of 2.8: The Interactive Interpreter (REPL)

The Scala Interpreter (often called a REPL for Read-Evaluate-Print Loop) sits in an unusual design space - an interactive interpreter for a statically typed language straddles two worlds which historically have been distinct.  In version 2.8 the REPL further exploits the unique possibilities.

Package and Class Completion

On startup, the REPL analyzes your classpath and creates a database of every visible package and path.  This is available via tab-completion analagous to the path-completion available in most shells.  If you type a partial path, tab will complete as far as it can and show you your options if there is more than one.

scala> scala.co<tab>
collection   compat       concurrent
scala> org.w3c.dom.DOMI<tab>
DOMImplementation         DOMImplementationList     DOMImplementationSource
scala> org.w3c.dom.DOMImplementation

Member Completion

Both static and instance methods of objects are also available.

scala> val x = "abc"
x: java.lang.String = abc
scala> x.re<tab>
regionMatches   replace         replaceAll      replaceFirst
scala> java.lang.String.c<tab>
scala> java.lang.String.copyValueOf

And because this is all accomplished on the fly via reflection, even anonymous classes you've only just defined are equally accessible.

scala> val x = new AnyRef { def kltpzyxm = "back to the 5th dimension" }
x: java.lang.Object{def kltpzyxm: java.lang.String} = $anon$1@9cb63b
scala> x.<tab>
asInstanceOf   getClass       isInstanceOf   kltpzyxm       toString

Interactive Debugging

The interpreter can now be conditionally launched from at an arbitrary point in a program, and make your program internals available interactively.  The arguments passed to Interpreter.breakIf can be inspected with tab-completion and even modified, with program execution resuming once you quit the REPL.

Coming Soon, Scala 2.8

There are many more minor improvements and conveniences in the REPL since the 2.7 release.  New features for the REPL are largely driven by user feedback, so take a nightly build for a spin and tell us what you'd like to see.

Re: A Taste of 2.8: The Interactive Interpreter (REPL)

This is really cool! Here are two more suggestions:

What about adding completion for the REPL internal commands like ":help", ":jar", ":load", ...?

What about adding completion for members coming from implicit conversions?

Re: A Taste of 2.8: The Interactive Interpreter (REPL)

I could do completion for commands, though it's not exactly necessary because they already work with the shortest unambiguous string - i.e. :h or :j work as well as :help or :jar.

Implicits has been on my list for a long time but until recently the compiler was not constructed to give simple access to the info I needed.  Happily the recent work on eclipse should make this task a lot easier, so it'll probably be in there for 2.8.

Re: A Taste of 2.8: The Interactive Interpreter (REPL)

Hi Paul, I think Michael meant autocompletion of the jar file names, not the command itself. I came here to suggest the same. ;)

Re: A Taste of 2.8: The Interactive Interpreter (REPL)

hi,

 

i noticed on older computers (G4 1 GHz) that the execution is still very slow. has the invocation of the compiler been changed for 2.8, i.e. is it using the fsc compiler server now? on my intel 2 ghz an execution of lets say "1+1" still feels sluggish compared to lets say groovy.

 

the autocomplete is very nice!

Re: A Taste of 2.8: The Interactive Interpreter (REPL)

Hello,

 

I am using Netbeans 6.7, Scala 2.7.5 and compilation time is very big !! I always have to divide my Scala project into few smaller projects and compile them separately to speed up things.

I noticed that if my project growes to 5-10 classes compilation time start to be very annoying.

 

My machine : Intel Core2Duo 2.6GHz 4 cores, DDR3, latest 64bit JVM.

 

Is there a chance that Scala 2.8 will compile faster ??

Thanks, Arthur.

Re: A Taste of 2.8: The Interactive Interpreter (REPL)

These look like very nice features.  Currently I am using the BeanShell  (www.beanshell.org) to extend my JVM applications with "interactive debugging" capabilities. During program execution I open up a BeanShell console (interactive Swing GUI) to inspect program variables, execute code, etc. It sounds like the Scala interpreter will have a similar capability, which is great!

BeanShell allows one to toggle the visibility of private fields; for debugging it's nice to get at these directly.  Will the embedded Scala interpreter also (optionally) allow this?

Will there be some utility class to launch the Scala interpreter inside a GUI window?

Where can we read more about using the Scala interpreter?

I'm looking forward to 2.8.

 

Re: A Taste of 2.8: The Interactive Interpreter (REPL)

Toggling private visibility is something I'd definitely like to do.  It's a bit tricky because the JVM will still enforce the access restrictions, so although I can make the tab completion show the names of the private fields, accessing them will fail unless I add a lot more intelligence to the interpreter (it would have to notice the attempt to exceed access and rewrite it as a reflective call.)

A repl GUI is on the wishlist but not high on my personal priority list as I have many other features clamoring to be implemented.

As to where to read about it, there's not a whole lot of documentation yet but I'll write an interpreter walkthrough before 2.8 is out.

Re: A Taste of 2.8: The Interactive Interpreter (REPL)

...accessing them will fail unless I add a lot more intelligence to the interpreter (it would have to notice the attempt to exceed access and rewrite it as a reflective call.)

 

Yes, that does sound tricky. How about a flag indicating that all field accesses (both public and private) be transformed into reflective calls? There would be the obvious performance penalty, but for debugging and inspection (which is when you'd want to access private fields) that's usually not an issue.

Re: A Taste of 2.8: The Interactive Interpreter (REPL)

 if it helps, scalatest has a private method invoker

Re: A Taste of 2.8: The Interactive Interpreter (REPL)

Thanks for the pointer.  The private method invoker is described here. I think that the syntax is the following

val result = targetObject invokePrivate (PrivateMethod[String]('targetMethod))(arg)

It's better than using the Java reflection libraries, but definitely not as concise as

val result = targetObject.targetMethod(arg)

Re: A Taste of 2.8: The Interactive Interpreter (REPL)

It appears that the reflective calls can be a little more concise.  Using the dyncall library it is possible to write

val result = targetObject-->'targetMethod(arg)

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland