如何从 repl 访问 scala 文档? [英] How do I access scala documentation from the repl?

查看:48
本文介绍了如何从 repl 访问 scala 文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先是内置文档,还有我自己的代码.

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

具体来说,我想获得类似于在 python 中如何在方法或对象上调用 help() 以获取有关打印到 repl 中的对象的信息的信息.

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.

推荐答案

Scaladoc 生成为 HTML,因此您不希望它们出现在 REPL 窗口中.但是,您可能希望在浏览器中从 REPL 加载文档.你可以像这样创建你自己的方法来做到这一点(这个方法采用一个实例;如果你愿意,你可以让它采用 Class[A] 的实例):

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
}

如果您想变得更聪明,您可以解析名称以将 Web 浏览器指向 Java 类的 Javadocs 和 Scala 类的 Scaladocs 以及您拥有类文档的任何位置.您可能还想使用本地源,file:///my/path/to/docs/index.html# 而不是来自网络的 API.但我用了这个,所以你可以试试

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))

这篇关于如何从 repl 访问 scala 文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆