Ki is a new interactive shell for Kotlin that aims to make it easier for developers to do quick experiments with the language and to take advantage of REPL-driven development.
Ki is not the only interactive shell to be available for Kotlin. Rather, it tries to overcome the limitations of existing Kotlin interactive shells, as JetBrains engineer Pasha Finkelshteyn explains:
kotlinc
does not have autocomplete or syntax highlighting. Kotlin REPL is very powerful, but it forces you to open IntelliJ IDEA, which is not always convenient.
Ki supports autocompletion, aimed to simplify access to Kotlin collection API; external dependencies, thanks to a special :dependsOn
construct that loads a dependency from Maven Central and makes it available locally; and type inference, with the added bonus of a built-in t
command which is able to show the type of any given expression.
Additionally, ki
provides a special paste mode to better handle large code snippets. This mode can be enabled by executing the command :paste
, which makes the REPL able to swallow whole chunks of code that are immediately interpreted and can be referenced later.
Another interesting feature of ki
is scripting support, which enables saving a session to disk and load it back using the :l
command. After loading a script, the :ls
command will show you a list of all imported symbols, including functions, classes, variables, etc. This is especially useful to spare oneself the chore of loading all required dependencies each time, says Finkelshteyn.
@file:DependsOn("com.GitHub.kittinunf.fuel:fuel:2.3.1")
import com.GitHub.kittinunf.fuel.*
import java.net.*
fun sendGet(address: String) {
val url = URL(address)
with(url.openConnection() as HttpURLConnection) {
requestMethod = "GET" // optional default is GET
println("Sent GET request to URL : $url; Response Code : $responseCode")
inputStream.bufferedReader().use {
it.lines().limit(5).forEach { line ->
println(line)
}
}
}
}
ki
is built on top of a plugin system that can be extended to cover specific scenarios. In fact, all ki
commands, such as :l
, :ls
, and :paste
, are implemented as plugins.
As a final note, JetBrains also created a dedicated version of ki
with specific support for Apache Spark. Its main advantage is it automatically creates a local Spark context on launch and makes running Apache Spark queries much faster.
ki
is open source and available on GitHub as well as as a binary distribution.