在Scala REPL中,用什么命令来检查实例?

5
在Python中:
>>> s = "abc"
>>> dir(s)
['__add__', '__class__', '__contains__', '__delattr__', ...

在Scala REPL中,是否有与dir()函数等效的方式来处理实例?

你需要比制表符补全更好的东西吗? - Debilski
@Debilski,是的,如果你使用的不是纯终端,例如SublimeREPL。 - Ufos
2个回答

8
当你按下制表键时,REPL会显示你可以在对象上调用的方法:
scala> val s = "abc"
s: java.lang.String = abc

scala> s.<tab>

+                     asInstanceOf          charAt
codePointAt           codePointBefore       codePointCount
compareTo             compareToIgnoreCase   concat
contains              contentEquals         endsWith
equalsIgnoreCase      getBytes              getChars
indexOf               intern                isEmpty
isInstanceOf          lastIndexOf           length
matches               offsetByCodePoints    regionMatches
replace               replaceAll            replaceFirst
split                 startsWith            subSequence
substring             toCharArray           toLowerCase
toString              toUpperCase           trim

想了解关于REPL的更多信息,请查看此处


7

为什么在REPL中使用tab键补全更为合适(一旦你输入方法名,它会显示参数),相当于dir的技术等效物是:

s.getClass.getMethods

不完全正确,因为dir()将获取所有内容,包括成员变量等。 - Mike Axiak
@Mike 那是无关紧要的,因为Scala除了作为一种实现工具之外,并没有成员变量。在Scala对象中可用的所有内容都可以作为方法使用。 - Daniel C. Sobral

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接