When you're working in the Scala REPL and want to see what methods are available on a class/object, you can create an instance of an object, follow that with the "." character, and then press the [Tab] key. This process, known as "tab completion" in the REPL, gives you a preliminary list of methods that can be called on the object.
Here's what this looks like when we try it on an Int object:
scala> 5.[Tab] % & * + - / > >= >> >>> ^ asInstanceOf isInstanceOf toByte toChar toDouble toFloat toInt toLong toShort toString unary_+ unary_- unary_~ |
I mentioned that this is a preliminary list, and what I meant by that is that if press [Tab] again, it will show even more methods, as you can see here:
scala> 5.[Tab][Tab] != ## % & * + - / < << <= == > >= >> >>> ^ asInstanceOf equals getClass hashCode isInstanceOf toByte toChar toDouble toFloat toInt toLong toShort toString unary_+ unary_- unary_~ |
How the two sets of methods are created
The way the methods are shown seems to currently be best described at this link on Stack Overflow. As the author there summarizes:
So with one tab you're getting the methods filtered by some rules that the interpreter developers have decided are reasonable and useful. Two tabs gives you the unfiltered version.
Example 2: Showing Scala String methods in the REPL
As another quick example of how this works, here are the methods the Scala REPL shows on a String object, first with one tab, and then with two:
scala> "foo".[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 scala> "foo".[Tab][Tab] != ## $asInstanceOf $isInstanceOf + == asInstanceOf charAt codePointAt codePointBefore codePointCount compareTo compareToIgnoreCase concat contains contentEquals endsWith eq equals equalsIgnoreCase getBytes getChars getClass hashCode indexOf intern isEmpty isInstanceOf lastIndexOf length matches ne notify notifyAll offsetByCodePoints regionMatches replace replaceAll replaceFirst split startsWith subSequence substring synchronized this toCharArray toLowerCase toString toUpperCase trim wait
Approaches to see implicit methods in the Scala REPL
I tried several different approaches to see if there was a simple way to list implicit methods that are available on an object, such as the methods in the StringOps class that are available to String objects. In short, I haven't found anything that works yet, other than what I described in my How to show Scala String and StringOps methods in the REPL examples.
There’s just one person behind this website; if this article was helpful (or interesting), I’d appreciate it if you’d share it. Thanks, Al.