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.
Related
The scala documentation shows that the way to create a scala script is like this:
#!/bin/sh
exec scala "$0" "$#"
!#
/* Script here */
I know that this executes scala with the name of the script file and the arguments passed to it, and that the scala command apparently knows to read a file that starts like this and ignore everything up to the reversed shebang !#
My question is: is there any reason why I should use this (rather verbose) format for a scala script, rather than just:
#!/bin/env scala
/* Script here */
This, as far a I can tell from a quick test, does exactly the same thing, but is less verbose.
How old is the documentation? Usually, this sort of thing (often referred to as 'the exec hack') was recommended before /bin/env was common, and this was the best way to get the functionality. Note that /usr/bin/env is more common than /bin/env, and ought to be used instead.
Note that it's /usr/bin/env, not /bin/env.
There are no benefits to using an intermediate shell instead of /usr/bin/env, except running in some rare antique Unix variants where env isn't in /usr/bin. Well, technically SCO still exists, but does Scala even run there?
However the advantage of the shell variant is that it gives an opportunity to tune what is executed, for example to add elements to PATH or CLASSPATH, or to add options such as -savecompiled to the interpreter (as shown in the manual). This may be why the documentation suggests the shell form.
I am not on the Scala development team and I don't know what the historical motivation for the Scala documentation was.
Scala did not always support /usr/bin/env. No particular reason for it, just, I imagine, the person who wrote the shell scripting support was not familiar with that syntax, back in the mid 00's. The documentation followed what was supported, and I added /usr/bin/env support at some point (iirc), but never bothered changing the documentation, it would seem.
For the times when you are reading source code without an IDE at hand.
Compile with the Xprint:typer scalac option.
This tells scalac to explain what it's doing.
This article explains the option and gives an example of the output (towards the bottom).
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.
I frequently use the Scala console to evaluate and test code before I actually write it down in my project. If I want to know the contents of a variable, I can just enter it and scala evaluates it. But is there also a way to show the code of methods I entered?
I know there's the UP-key to show single lines, but what I was searching for is to show the whole code at once.
There's a file in your home directory named .scala_history that contains all of your recent REPL history. I regularly copy and paste code from this file into project source files. It's not exactly the same as showing the code for individual methods in the REPL, but it might help you accomplish the same goals.
See the comments by Paul Phillips in this issue for a discussion of some related functionality in the REPL (grouping statements in the history):
At some point I implemented the logic for this, but the real obstacle
is jline. It has enough trouble figuring out where the cursor is under
the simplest conditions. Start throwing big multiline blocks into the
history and it breaks down in tears. Would love to see this and
SI-2547 addressed by the community.
...
I expect to fix this soon too, but it depends on how well the recent
jline work goes. I implemented it long ago, and display issues are the
impediment.
Both of these comments are over two years old, so I wouldn't hold your breath.
I dont know a command to load all the code from command line.
What you can do is to :load path/to/my/file.scala to load some complex code and re- :load it when you changed the code in the file.
I wrote a Lisp function earlier that had an error. The first challenge was to figure out how to view the function again. That challenge is solved. Now that I see WHAT I have done wrong, I want to modify the contents of the defined function without rewriting the whole thing?
Seems like as intelligent as Lisp is, there HAS to be a way to do this, I just don't know what it is because I am fairly new to the language. Can this be done?
Judging from the question, I think that you have a strange setup. It seems to indicate that you are writing your functions directly at the REPL. Don't do that.
The usual setup is to have an IDE (for example, Emacs with Slime) where you edit a source file, and then "send" top-level forms (like function definitions) to the REPL.
Every useful REPL has a history functionality. It allows you to move in the history of your input backwards and forwards.
When I write code in the REPL simple keystrokes like m-p gets back earlier code. Some IDEs might even be able to locate source code in a Lisp listener with m-. .
In most REPLS you can also search incrementally backwards.
If you want a log of your input use the function DRIBBLE..
There are some more options, like retrieving the code from the function - when a Lisp IDE supports that.
There is the advice functionality in many Lisps, which lets you run additional code before or after or around an existing function. But the comment is right, why wouldn't you rewrite a function if you're still learning and trying things out? Do they charge you by the compile cycle?