Tech TLDR;

  • Archive
  • Top Posts
  • GitHub
  • LinkedIn
  • Contact

Write or Paste Multi-Line Code in Scala REPL

April 7, 2017 by admin

I love Python and ipython, so I often crave similar experience when working with Scala.

Scala already has a built in REPL which is a huge plus, however, I wasn’t sure how to copy/paste multi-line text.

For example, I had a snippet of code saved somewhere that looked as such:

someList
   .map(someFunction)
Code language: CSS (css)

This would work fine if entered like this:

someList.map(someFunction)
Code language: CSS (css)

But Scala would evaluate the someList first, if input was entered across multiple lines.

Turns out, Scala REPL has a :paste command to support a paste mode. So you can just do:

:paste // Hit enter

someList
   .map(someFunction)

//Hit ctr+D to execute block of code
Code language: JavaScript (javascript)

I often use Ammonite, an ipython like took for Scala REPL. It suffers from the same problem, but does not support the :paste command.

Similar behavior, however, can be achieved by simple wrapping your code in brackets, as such:

{
someList
   .map()
}

Wrapping the code in {}, unfortunately will make tab completion useless. So while this works great for copy/pasting code, it does not work when I am trying to prototype something, and would like to spread it out across multiple lines.

If you know how to write . chained functions across multiple line AND keep the tab completion working, or any other cool Scala REPL tricks, please let me know in the comment area bellow.

Filed Under: Scala

Copyright © 2023 · eleven40 Pro Theme on Genesis Framework · WordPress · Log in