Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

repl







Scala REPL

  • Overview: The Scala REPL is an interactive command-line interface for Scala. It reads Scala expressions, evaluates them, and prints the results, making it a useful tool for experimentation and learning.
  • Starting the REPL: Typically, you start the REPL by typing scala in your command line after Scala has been installed on your system. This launches an interactive session where you can type and evaluate Scala expressions.
  • Basic Usage: In the REPL, you can type Scala expressions or definitions, and they will be immediately compiled and executed. The REPL provides feedback on the types and values of the expressions.







Basic Operations

  • Evaluating Expressions: Type any valid Scala expression, and the REPL will evaluate it, showing the result and type.
    scala> 1 + 1
    res0: Int = 2
    
  • Defining Variables and Functions: You can define variables and functions directly in the REPL.
    scala> val x = 5
    x: Int = 5
    
    scala> def addOne(a: Int) = a + 1
    addOne: (a: Int)Int
    
  • Loading External Libraries: Use :require or :load to include external Scala files or libraries in your REPL session for testing or exploration.





Advanced Features

  • Tab Completion: The REPL supports tab completion, which can help you find method names, variable names, and more.
  • Inspecting Types: Use :type to inspect the type of an expression without evaluating it.
    scala> :type addOne
    Int => Int
    
  • Viewing Imported Names: Use :imports to see a list of all imported names in the current session.







Customizing Scala REPL

  • Custom Initialization: You can customize the REPL environment through a .scala file in your home directory named .scalarc that contains Scala code to be executed at startup.
  • REPL Command History: The REPL keeps a history of commands. You can navigate through this history using the up and down arrow keys, and search through it using Ctrl+R.







Integrating with IDEs

  • IDE Support: Many Integrated Development Environments (IDEs) offer integrated REPL tools, providing a seamless development experience by combining the benefits of the REPL with the features of the IDE.
  • Ammonite REPL: An alternative to the Scala REPL, Ammonite offers additional features like better syntax highlighting, multi-line editing, and more powerful integration capabilities.

#





Best Practices

  • Experimentation and Learning: Use the REPL as a playground for learning new Scala features, experimenting with language constructs, or testing library functions.
  • Rapid Prototyping: Quickly prototype functions and algorithms in the REPL before integrating them into a larger Scala project.
  • Debugging and Testing: Test small pieces of code or expressions related to a larger problem you're debugging.







Limitations

  • Performance: The REPL is not optimized for performance testing. The evaluation time in the REPL may not accurately reflect the performance of compiled Scala code.
  • State Management: In complex sessions, managing the state and imports can become cumbersome. Restarting the REPL session can sometimes be necessary to clear the state.