改进翻译

Running Code Snippets

最近更新 2019-11-13
This tutorial shows the ways to write and run Kotlin code snippets in a lightweight manner without creating entire applications.

There may be times when you need to quickly write and execute code outside of a project or application. This may be useful, for example, when learning Kotlin or evaluating expressions. Let's have a look at three handy ways we can use to run Kotlin code quickly:

  • Scratches let us write and run code in a temporary file outside of our project in the IDE.
  • Worksheets are like scratches, but they reside within projects.
  • REPL (Read-Eval-Print-Loop) runs code in an interactive console.

Scratches and worksheets

The Kotlin plugin for IntelliJ IDEA supports scratches and worksheets.

Scratches let us create code drafts in the same IDE window as our project and run them on the fly. Scratches are not tied to projects; you can access and run all your scratches from any IntelliJ IDEA window on your OS.

To create a Kotlin scratch, click File | New | Scratch file and select the Kotlin type.

In turn, worksheets are project files: they are stored in project directories and tied to the project modules. Worksheets are useful for writing code parts that don't actually make a software unit but should still be stored together in a project. For example, you can use worksheets for education or demo materials.

To create a Kotlin worksheet in a project directory, right-click the directory in the project tree and select New | Kotlin Worksheet.

In scratches and worksheets, you can write any valid Kotlin code. Syntax highlighting, auto-completion, and the other IntelliJ IDEA code editing features are all supported too. Note that there's no need for declaring the main function: all the code you write is executed as if it would be in the body of main.

Once you have finished writing your code in a scratch or a worksheet, click Run. The execution results will appear in the lines opposite your code.

交互模式

IntelliJ IDEA can run the code from scratches and worksheets automatically. To get the execution results when you stop typing, switch on the Interactive mode.

调用模块

We can use classes or functions from a Kotlin project, in our scratches and worksheets.

Worksheets automatically get access to classes and functions from the module where they reside.

To use classes or functions from a project in a scratch, import them into the scratch file as usual with the import statement. Then write your code and run it with the appropriate module selected in Use classpath of module list.

Both scratches and worksheets use the compiled versions of connected modules. So, if you modify a module's source files, the changes will propagate to scratches and worksheets when you rebuild the module. To rebuild the module automatically before each run of a scratch or a worksheet, select Make before Run.

Scratch select module

像 REPL 那样运行

To evaluate each particular expression in a scratch or a worksheet, run it with Use REPL selected. The code will be executed the same way as in REPL: the code lines will run sequentially, providing results of each call. You can later refer to the results by the names res* shown in the corresponding lines.

Scratch REPL

REPL

REPL (读取-求值-输出-循环) 是一个交互式运行 Kotlin 代码的工具。REPL 允许你运行表达式与代码块,而无需创建项目或函数(如果你不需要的话)。

如需在 IntelliJ IDEA 中运行 REPL,请打开 工具 | Kotlin | Kotlin REPL

如需在操作系统命令行中运行REPL,请从 Kotlin 独立编译器的目录中打开 __/bin/Kotlic-JVM __ 。

The REPL command line interface will open. You can enter any valid Kotlin code and see the result. The results are printed as variables with auto-generated names like res*. You can later use such variables in the code you run in REPL.

REPL supports multiline input as well. The result of the multiline input is the value of its last expression.