CodeX
Published in

CodeX

CODEX

A quick little note about tab completion on the local Scala REPL

Photo by Christin Hume on Unsplash

Completion suggestions are very helpful when programming Scala on an integrated development environment (IDE) like IntelliJ IDEA. And a REPL (read-eval-print-loop) like the local Scala REPL is a great way to quickly try out things that may or may not work.

Too bad there are no completion suggestions on the local Scala REPL. Actually, there are…

It requires pressing a key, specifically the Tab key. It’s kind of like completion with Ctrl- or Command-Space on Eclipse and NetBeans.

Well, it depends on your setup. Apparently it’s possible in some setups for the local Scala REPL to mostly work but lack tab completion (I write “local Scala REPL” to distinguish from an online REPL like Scastie).

Does tab completion work on your system? We’ll see soon enough. Get your local Scala REPL going if you don’t already have it up and running, do a couple of calculations with anything.

C:\Users\AL>scala
Welcome to Scala 2.13.1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_281).
Type in expressions for evaluation. Or try :help.
scala> Long.MaxValue : BigInt
res0: BigInt = 9223372036854775807
scala> res0 * 2
res1: scala.math.BigInt = 18446744073709551614

Okay, so far, so good. But I forget what functions and procedures I can use on BigInt instances. So I type “res1” followed by a period, then I press Tab.

scala> res1.
% < bigInteger doubleValue isValidByte longValue setBit toChar unary_~
& << bitCount equals isValidChar lowestSetBit shortValue toDouble underlying
&~ <= bitLength flipBit isValidDouble max sign toFloat until
* > byteValue floatValue isValidFloat min signum toInt |
+ >= charValue gcd isValidInt mod testBit toLong
- >> clearBit hashCode isValidLong modInverse to toShort
/ ^ compare intValue isValidShort modPow toByte toString
/% abs compareTo isProbablePrime isWhole pow toByteArray unary_-
scala> res1.

Notice that the Scala REPL fills in what you had already typed prior to invoking tab completion, so that you don’t have to type it again. But you still have to type whatever it is that you select from the suggestions.

I’m going to try out bitLength() and gcd():

scala> res1.gcd(res0)
res3: scala.math.BigInt = 9223372036854775807
scala> res1.bitLength
res4: Int = 64

I find it mildly interesting that res2 was skipped over. You might or might not also find that mildly interesting.

Huh, what’s that underlying() function do?

scala> res1.underlying
res5: java.math.BigInteger = 18446744073709551614

Oh, right, it retrieves the underlying Java BigInteger.

I can narrow down the suggestions if I know the first few letters of what I want, like, for the example, the “is” functions:

scala> res1.is
isInstanceOf isValidByte isValidDouble isValidInt isValidShort
isProbablePrime isValidChar isValidFloat isValidLong isWhole
scala> res1.is

As before, the Scala REPL fills in what we typed before so we don’t have to type it again.

This doesn’t tell us what parameters these functions take, but since we’re in a REPL, the worst thing that can happen is that we get an error message.

scala> res1.isProbablePrime
^
error: missing argument list for method isProbablePrime in class BigInt
Unapplied methods are only converted to functions when a function type is expected.
You can make this conversion explicit by writing `isProbablePrime _` or `isProbablePrime(_)` instead of `isProbablePrime`.
scala>

We can also broaden the suggestions by pressing Tab twice, so that the completion suggestions also include items that are inherited from superclasses without overriding, such as wait() from java.lang.Object.

scala> res1.
% < bigInteger doubleValue isValidByte longValue setBit toChar unary_~
& << bitCount equals isValidChar lowestSetBit shortValue toDouble underlying
&~ <= bitLength flipBit isValidDouble max sign toFloat until
* > byteValue floatValue isValidFloat min signum toInt |
+ >= charValue gcd isValidInt mod testBit toLong
- >> clearBit hashCode isValidLong modInverse to toShort
/ ^ compare intValue isValidShort modPow toByte toString
/% abs compareTo isProbablePrime isWhole pow toByteArray unary_-
scala> res1.
!= / ^ compare gcd isValidFloat mod sign toFloat wait
## /% abs compareTo getClass isValidInt modInverse signum toInt |
% < asInstanceOf doubleValue hashCode isValidLong modPow synchronized toLong ?
& << bigInteger ensuring intValue isValidShort ne testBit toShort
&~ <= bitCount eq isInstanceOf isWhole notify to toString
* == bitLength equals isProbablePrime longValue notifyAll toByte unary_-
+ > byteValue flipBit isValidByte lowestSetBit pow toByteArray unary_~
- >= charValue floatValue isValidChar max setBit toChar underlying
-> >> clearBit formatted isValidDouble min shortValue toDouble until
scala> res1.

I find that this comes in handy both for standard Java and Scala classes as well as for classes that I write.

Sometimes you might decide, from your REPL use, that you don’t like the name you gave a particular function. You might go on IntelliJ and change the function’s name. If you do that, keep in mind that you’ll have to quit and restart the REPL for the changes to be reflected.

--

--

--

Everything connected with Tech & Code. Follow to join our 900K+ monthly readers

Recommended from Medium

Azure Cognitive Services

Unite Us Technology Team Member Spotlight: Tony Deng

Part 2: Introduction to Kafka

Ansible’s Contents

Beginner Android — Constraint Layouts

Software is Eating the Self

Encentive & Astar Network Strategic Partnership Announced!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alonso Del Arte

Alonso Del Arte

is a composer and photographer from Detroit, Michigan. He has been working on a Java program to display certain mathematical diagrams.

More from Medium

Scala: for-comprehension stops at the empty box

Polymorphism in Scala

Type Classes in Scala 2

Achieving Indisputable Job Security Using Novel Scala 3 Features: A Case Study