8

There are chances that some typing (in Ubuntu terminal - scala -version Scala code runner version 2.9.1) errors occur, example shown below where instead of (l: Int) (l: int) has been typed in parameters.

 scala> class Rectangle (l:int, w: Int){
 | val length = l
 | val length = w
    .
    .
    .
    few more lines but still ... module Rectangle is not defined. Or sometimes enter command given and error shows up.

Is there anyway where directly that typing error can be edited / modified and rerun the code? It will save great deal of time otherwise I am entering line by line using up/down arrow.

Please guide.

Mario Galic
  • 47,285
  • 6
  • 56
  • 98
Optimight
  • 2,989
  • 6
  • 30
  • 48
  • 1
    I think after entering into the :paste mode and pasting the code content (which can be copied from terminal itself or from any other source) editing should be possible. – Optimight Jun 06 '12 at 11:45
  • 1
    In the REPL there is `:load` to load some Scala source files. – kiritsuku Jun 06 '12 at 12:49
  • Anyone learning scala at very beginning stage or in requirement of evaluating tiny code samples should install scalaconsole. For more details please review the guidance given by Daniel C. Sobral during chat at http://chat.stackoverflow.com/transcript/12284 – Optimight Jun 08 '12 at 06:48

3 Answers3

4

I don't know if there is a better solution, but in my case I tend to use my usual text editor to write code snippet and paste them using the paste mode of the REPL (you can enter the paste mode thanks to the :paste command).

Nicolas
  • 24,509
  • 5
  • 60
  • 66
  • I tried Libre Office in similar manner. Process I followed (1) copied from terminal (2) Make necessary edit (3) Copy back to terminal --- but it sometimes creates errors for each line. Requires to remove enter events from each line during step (2) mentioned above. It also becomes very time consuming and not an effective way. – Optimight Jun 06 '12 at 11:23
  • 1
    Two advices then: 1. use a better tool to edit the code (vim, emacs, notepad++, sublimetext, textmate, jedit...) 2. Start editing your code into the editor, not into the REPL. Enter code directly into the REPL only for oneliner. – Nicolas Jun 06 '12 at 11:28
  • Are you suggesting continuous switching between edit tool (for editing) and then go back to REPL to run the code? I think after entering into the :paste mode, editing should be possible, so no other application (eitor) is needed. – Optimight Jun 06 '12 at 11:39
  • Yes. But.... Keep in mind that most of the time, REPL is here only to tests things or to provide you a quickl eveluation. If you want to build a bigger application, you'd better use command line to compile/interpret your code in some scala files. – Nicolas Jun 06 '12 at 11:53
  • Without switching or using any editor. You could even write down the code in the :paste context. BUT use a text editor... Libre Office isn't, actually when you copy from Libre Office I expect to have your clipboard polluted by Formatting content (the simplest is the font-familly, ...) – Andy Petrella Jun 06 '12 at 13:26
  • @andypetrella Actually, you cant type directly in paste mode, but you won't be able to correct previous lines and that was the original question. – Nicolas Jun 06 '12 at 13:51
  • @Nicolas you're right but I wanted to tell that's possible. But what I really wanted to do is explain why LibreOffice is not a good choice (and what happend with the REPL) – Andy Petrella Jun 06 '12 at 14:02
  • @Nicolas I am just a beginer and trying to run code samples given in the books (1) Steps in Scala: An Introduction to Object-Functional Programming (2)Programming in Scala. – Optimight Jun 06 '12 at 18:01
  • @nicolas hi, right now I am using Scala IDE for Eclipse and experimenting with the scala code with overall much greater speed than I was able to do using REPL – Optimight Jun 07 '12 at 17:26
2

Right now, you can't. There are Scala GUI REPL's (see them here, plus kojo and the big IDEs), though, which allow this kind of thing. Pick one of them.

Daniel C. Sobral
  • 295,120
  • 86
  • 501
  • 681
  • The `ScalaConsole` listed in your reference link is outdated, try [this new one](https://bitbucket.org/centaur/scalaconsole/wiki/Home). – xiefei Jun 07 '12 at 02:51
  • @Daaniel C. Sobral I have just downloaded Scala IDE for Eclipse 2.0.1 & trying to learn it. – Optimight Jun 07 '12 at 03:34
  • @Optimight I thought you specifically wanted something lighter than Eclipse&cia, but if that works for you, its a good choice. And it does have a REPL mode, which is basic but improving (try out the nightly versions from Scala IDE). I was thinking more of something like ScalaConsole, but it has to be something that works for you, and this is very subjective. – Daniel C. Sobral Jun 08 '12 at 03:54
  • @ Deaniel C Sobral I definitely want the lightest and fastest option. I have started learning Scala since a week. I am open for exploring all avenues. Please note that I am right now struggling with Scala Eclipse IDE. Facing some weired behaviour or my knowledge/logic is limited to operate IDE. – Optimight Jun 08 '12 at 04:15
  • @Optimight Try ScalaConsole out, then. It's light, and seems to do what you want. The link to it in the wiki was broken, but xiefei above has provided a recent link. – Daniel C. Sobral Jun 08 '12 at 04:19
  • @xiefei Thanks. I have fixed it on the wiki, but I'd like to point out that it _is_ a wiki. You could have fixed it yourself -- just create an account if you don't have one. – Daniel C. Sobral Jun 08 '12 at 04:20
  • @ Daniel C. Sobral Can we have a chat or Can I have your mail ID? I need help to speed up the Scala Learning. I will be greatful. – Optimight Jun 08 '12 at 05:00
  • @Optimight let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/12284/discussion-between-daniel-c-sobral-and-optimight) – Daniel C. Sobral Jun 08 '12 at 05:05
1

Since Scala 2.13.2 multi-line editing is supported in the REPL based on JLine 3

JLine 3 supports multi-line editing, a better tab-completion UI, and more.

Configure keybindings with -Xjline:emacs (the default) or -Xjline:vi; disable with -Xjline:off

History file is now ~/.scala_history_jline3

For example, to try multiline editing using vim keybindings start the REPL like so

scala -Xjline:vi

then enter a multiline definition and press up arrow key. Note how it gives

scala> class Foo {
     |   val x = 42
     | }
class Foo

scala> class Foo {
     |   val x = 42
     | }

instead of the old behaviour

scala> class Foo {
     |   val x = 42
     | }
defined class Foo

scala> }
Mario Galic
  • 47,285
  • 6
  • 56
  • 98