if I write in scala 2.10 REPL (interactive Scala shell):
"""\w""".
And press TAB it gives me:
+ asInstanceOf charAt
codePointAt codePointBefore
codePointCount compareTo compareToIgnoreCase
concat contains ....
However, .r is missing. When I put the same string into eclipse, it offers me .r as well. The same is true if I insert import scala.util.matching._ before.
Why REPL is not offering all possibilities?
Even bigger problem REPL has if i try to work with unicode, e.g. I write:
"""\p{L}""".
and press TAB
it gives me error:
scala> """\p{L}""".<console>:1: error: unclosed multi-line string literal
"""
^
Again, it works fine in Eclipse.
Is REPL so buggy, or am I missing something?
Yes, r is missing, but if you write """\w""".r and press enter it nevertheless works res0: scala.util.matching.Regex = \w. Having tab autocompletion for r seems not really neccessary.
The unicode issue is probably caused by java. You can explicitly request UTF-8 if you pass -Dfile.encoding=UTF-8 to java. Here is a post which describes how to do it.
If you use Eclipse, I can reccommend the Scala worksheet plugin which is a very good repl replacement.
The REPL only displays fields and methods of the object, while .r is only available through an implicit conversion (augmentString in scala.Predef) which turns it into a StringOps. There is probably no reason for this besides the fact that it would need to be implemented and nobody got around to doing it. You can still call .r on this, of course.
The Scala IDE is smart enough to resolve implicits, which is why you can see it there.
Related
In Intellij Scala Worksheet support, what is the difference between the Run types i.e PLAIN vs REPL ?
Plain evaluation model compiles the whole worksheet in one go before evaluating expressions, whilst REPL evaluation model evaluates each expression on the go before moving to the next one.
Adding an expression in REPL mode evaluates incrementally just that new expression, whilst in Plain mode it would re-interpret the whole worksheet from the beginning.
An example where the difference matters is when defining companion objects. Similarly to how in Scala REPL proper we have to use :paste command to define companion, in IntelliJ Scala Worksheet we have to use Plain run type.
REPL mode as it says READ EVALUATE PRINT LOOP is kind of interpreter i.e. each expression will be evaluated after to move to next line.. It is generally used to make quick logic checks.
while in worksheet mode you need to make an object or class.. worksheet is the traditional OOPS way like we do in java and whole file is compiled in one go.
In IntelliJ Scala's debugger, you can open a window to evaluate expressions (alt + F8)
However, it always fails as long as I have Scala symbols in the expression (i.e., 'foobar), saying:
result = Literal has null value
It was a bug. It is fixed now, so all should work in the next version of the scala plugin.
You can use bugtracker for this kind of questions: http://youtrack.jetbrains.com/issue/SCL-6999
A single of code can achieve a lot in Scala.
def -(that: Nat) = if (that.isZero) this else throw new Error("negative number")
However, it is difficult to debug.
Any tips?
Use scala worksheet. That's it: you'll get multiline REPL with your environment in which you can play with your code.
Alternatively, just use REPL bundled with sbt (sbt console) with proper imports
you can see output of compiler phases, i.e. the AST after desugarings with
scalac -Xprint:typer
scalac -Xprint-types (note hyphen, not colon)
The man page is a little confusing, there's no "typer" phase listed, but it works:
http://www.scala-lang.org/docu/files/tools/scalac.html
Compile time type tracing
I have a file with several lines of scala code -- imports, list value assignments, etc. that I often use to initialize some things when using the REPL.
Currently I just open up the file in a text editor and copy-and-paste it into the REPL, but is there a way to do it by calling the external file in a more direct manner so I don't have to copy-and-paste every time?
In some interactive database tools like SQL Plus, it is done by typing #filename at the prompt. Is there something similar in the Scala REPL? Preceding the filename with # doesn't work, eval doesn't work either.
Type:
:help
and you see, that
:load <path> load and interpret a Scala file
solves your problem.
In some circumstances, pasting the code might be preferable though, but then
:paste
might be your friend then. It helps inserting a whole block without feedback, line by line, until you hit Ctrl + D. In some cases this is significant for the code interpretation.
I've been experimenting with Scala lately and I noticed that when I need to look up a function or a class, I have to go to the website or navigate to the local documentation.
Is there a way to read the Scaladoc from inside the interpreter?
Maybe something like help() in Python.
Scalaex is a cool Scala-web documentation like Hoogle with an usefull CLI-client.
(But it's not inside the interpreter. Unfortunately I cannot write a comment to your question, so I write this as an answer.)
I'm running the command line scala interpreter for 2.9, and tab-key completion works for me:
val x = List(1,2,3)
x.
Then press TAB
Shows the list of methods.
Not quite full docs, but helpful nonetheless.