You can define variables and methods. They should belong to some parent class. And, indeed, this.getClass prints something. But, it is something weired. What is the current context? How do you switch the context to another object? For instance, I see that scala combinators demand a method per parser, which means that you need to work with large (multiline) classes. How do you enter into the scope of that object to tweak it with the command line?
Asked
Active
Viewed 636 times
1
-
REPL is creating multiple nested scopes, one level down for each evaluation. That's why you can redefine (shadow) previous `val` or `def`. You can't "tweak" an object, Scala is a compiled language and creates objects once and for all. – Victor Moroz Jun 22 '16 at 22:47
1 Answers
2
You can enter classes at the prompt or in :paste
mode.
$ scala
Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_65).
Type in expressions for evaluation. Or try :help.
scala> class C
defined class C
scala> :pa
// Entering paste mode (ctrl-D to finish)
class C {
val c = 42
}
// Exiting paste mode, now interpreting.
defined class C
scala>
With EDITOR
set in your environment, you can edit history.
On this old Mac,
$ EDITOR=/Applications/TextEdit.app/Contents/MacOS/TextEdit
then
scala> :hi
657 Future(42)
658 .value
659 class C
660 :pa
661 class C {
662 val c = 42
663 }
scala> :ed 661-

som-snytt
- 39,429
- 2
- 47
- 129
-
Pasting whole classes defeats the whole purpose of (command-line) interpreter. You access SO using a browser. Take Chrome and set a debugger breakpoint in some javascript function. See how you can REPL - evaluate and set values in the context of the function, using console commands. I do not understand how editing the history makes this possible. – Little Alien Jun 22 '16 at 23:05
-
By the way, 2.12 has a ScriptEngine for setting arbitrary context dynamically; but normally in the REPL, you'd just define some vars and refer to them. – som-snytt Jun 23 '16 at 03:28
-
@som-snytt Regarding _"ScriptEngine for setting arbitrary context dynamically",_ can it be used to solve [Nested environments in Scala REPL](http://stackoverflow.com/questions/42499466/)? – Tomas Mikula Feb 28 '17 at 03:14