Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What command to use to introspect instances in scala REPL?

Tags:

python

scala

In python:

>>> s = "abc"
>>> dir(s)
['__add__', '__class__', '__contains__', '__delattr__', ...

Is there an equivalent way - i.e. dir() function - to do this with instances in the scala REPL ?

like image 395
Frankie Ribery Avatar asked Jan 15 '11 09:01

Frankie Ribery


2 Answers

When you press the tabulator-key the REPL shows you the methods which you can call on an object:

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

For more information about the REPL look here.

like image 126
kiritsuku Avatar answered Oct 19 '22 11:10

kiritsuku


Why tab-completion is more adequate as a help from within REPL (and it shows the parameters too, once you have written the method name!), the technical equivalent to dir is:

s.getClass.getMethods
like image 20
Daniel C. Sobral Avatar answered Oct 19 '22 10:10

Daniel C. Sobral