David Matuszek
David Matuszek

Reputation: 1263

How to echo user input in Scala REPL?

I'm teaching an introductory programming class, using Scala. We are starting with the REPL. The REPL has a bug in that, when the student enters a readLine command, their input is not echoed. Is there some workaround that I can suggest or provide?

I don't have this trouble when using Eclipse, but it will be some weeks before I introduce Eclipse to my students.

Upvotes: 8

Views: 1236

Answers (2)

Marth
Marth

Reputation: 24822

Use scala -Xnojline :

scala> val l = readLine
test
l: String = test

This does however break some things, in particular arrow keys, so you can't modify previous commands.

If available, you can use rlwrap scala -Xnojline (should be available on cygwin too) to restore those functionalities.

Full credit to this post.

Upvotes: 2

gourlaysama
gourlaysama

Reputation: 11290

You can use power mode to get access to the REPL's reader; it will give you a fully working readLine:

scala> :power
** Power User mode enabled - BEEP WHIR GYVE **
** :phase has been set to 'typer'.          **
** scala.tools.nsc._ has been imported      **
** global._, definitions._ also imported    **
** Try  :help, :vals, power.<tab>           **

scala> repl.in.readLine("enter something: ")
enter something: hello world
res0: String = hello world

scala>

Edit: as @som-snytt pointed out, in 2.11 you can use reader instead of repl.in in the above code, which is both shorter and easier to remember.

Upvotes: 4

Related Questions