31

First of all for the built in docs, and also for my own code.

Specifically, I want to get information similar to how in python you can call help() on a method or object to get information on just that object printed into the repl.

wn-
  • 1,475
  • 1
  • 14
  • 14
  • see also http://stackoverflow.com/questions/4160745/is-there-a-command-line-program-to-lookup-scaladoc – ax. Jul 20 '11 at 21:23
  • 2
    This question *is not* an exact duplicate of question 4160745. It's a question about Scala REPL not command line. – paradigmatic Jul 21 '11 at 06:43

1 Answers1

21

Scaladocs are generated as HTML, so you don't want them appearing in the REPL window. You might want to load docs in a browser from the REPL, however. You can do that by creating your own method like so (this one takes an instance; you could have it take an instance of Class[A] instead, if you prefer):

def viewdoc[A](a: A) {
  val name = a.asInstanceOf[AnyRef].getClass.getName
  val url = "http://www.scala-lang.org/api/current/index.html#"+name
  val pb = new ProcessBuilder("firefox",url)
  val p = pb.start
  p.waitFor
}

If you want to get extra-clever, you could parse the name to point the web browser at Javadocs for java classes and Scaladocs for Scala classes and wherever you have your documentation for your classes. You also probably want to use a local source, file:///my/path/to/docs/index.html# instead of the API from the web. But I used this so you can try out

scala> viewdoc(Some(1))
Rex Kerr
  • 166,841
  • 26
  • 322
  • 407
  • 1
    Just a short note: `firefox` must be in the path and you have to add `.exe` on a Windows box. – michael.kebe Jul 21 '11 at 05:33
  • 1
    @michael.kebe - Yes, and you must have firefox installed, or use another web browser from the command-line, etc. :) – Rex Kerr Jul 21 '11 at 13:38
  • This is cool, but not really what I was looking for - I've updated my question with more detail. – wn- Jul 25 '11 at 08:46
  • @wn-: I already addressed that in my answer. Scaladocs are in HTML. They're not in text. Are you asking how to get HTML in the REPL? How to convert HTML to text? Are you asking why the docs are in HTML not text? – Rex Kerr Jul 25 '11 at 14:14
  • 9
    Sorry for the confusion - the scaladoc output may be html, but the documentation abstractly is not html - it's annotated text. I would like to see that text (in some sort of shell friendly display format) in the repl shell when I want to learn what methods do. This would also be handy for seeing source code (both for rapid iteration and development). – wn- Jul 26 '11 at 18:39
  • @wn-: You can't follow links in the REPL. You can't have searching in the REPL. You can't have multiple windows open at once in the REPL. Maybe this is why the approach you want doesn't exist presently. It could be created with a bit of effort with a HTML translator. – Rex Kerr Jul 26 '11 at 18:43
  • 4
    None of that is needed (or probably desirable). If you've used ipython you know what I mean, otherwise see http://scienceoss.com/getting-help-in-ipython/ and try it for an example of what is possible (and awesome). I was hoping something like ?? existed, or would be easy to do in scala. – wn- Jul 26 '11 at 20:17
  • @wn-: That would be nice in some contexts, but it doesn't exist in Scala. I know it's more comfortable to use the same patterns of behavior you're used to with regard to looking up documentation, but the existing scaladocs in a browser work pretty well (even without the trick I showed in my answer). – Rex Kerr Jul 26 '11 at 21:08
  • You should adapt your answer to use `scala.sys.process`. ;-) – Daniel C. Sobral Oct 24 '11 at 16:04
  • @Rex What about my naturally indolent and lazy nature? – Daniel C. Sobral Oct 27 '11 at 19:54
  • 2
    For `Google Chrome` in `Mac OS X`, use this `ProcessBuilder` command `val pb = new ProcessBuilder("open", "-a", "Google Chrome", url)` – Hanxue Nov 14 '13 at 08:13
  • 1
    Tried it with viewdoc(List(1,2,3)) - failed. Much as I like the type safety of Scala compared to Python's "dynamic anarchy", the Scala REPL is currently far behind Python's (just try `a = [1,2,3] ; help(a)` in a default Python shell) - and really, REALLY far behind from jewels like iPython. I hope things will change in the future... – ttsiodras Nov 29 '14 at 09:13
  • @ttsiodras - I think most of the effort in Scala is going into making IDEs not REPLs alone. – Rex Kerr Nov 29 '14 at 09:21
  • 1
    @RexKerr: No question about it - it shows in the basic quality of the Scala REPL. It's still nice to have, though - and for people like me it helps a lot in easily figuring out the language constructs. I just wish I had online help from inside it, like I do for Python - it would speed up things. – ttsiodras Dec 01 '14 at 15:02
  • @ttsiodras - It is also possible to use an IDE. – Rex Kerr Dec 01 '14 at 20:43
  • I agree with @ttsiodras, coming from python scala repl feels unusable. Using an IDE (I do) is not a proper alternative as its purpose is just not the same: code package/application vs efficient test and prototyping. Scala is just younger. But it is also smart enough to follow good example. It'll come in time – Juh_ Aug 20 '15 at 16:50
  • Exposing only the HTML form is a poor design choice IMO. Interactive development in the REPL is not the only potential use for dynamic lookup of the raw annotated string form of Scaladocs. I want the Scaladocs for a tool that generates wrapper code to call Scala APIs (a la swig). Since the docs are only exposed as HTML, I am forced to look at scraping the HTML, which feels *bad*, but not quite as bad as forking Scaladoc itself just to make it cough up the useful goodies it fails to expose. – Blake Miller Nov 16 '16 at 05:00
  • I would love something like ocaml's utop. Input function, output method signature. With a bit of experimentation, it's easy to figure out what's happening. – Geoff Langenderfer Jun 10 '21 at 14:26