官术网_书友最值得收藏!

The REPL

These days, most languages provide an interactive shell, and Kotlin is no exception. If you want to quickly write some code that you won't use again, then the REPL is a good tool to have. Some people prefer to test their methods quickly, but you should always write unit tests rather than using the REPL to validate that the output is correct.

Note: REPL is the common name when referring to an interactive shell, and is an abbreviation for read, evaluate, print, loop.

You can start the REPL by adding dependencies to the classpath in order to make them available within the instance. To look at an example, we will use the Joda library to deal with the date and time. First, we need to download the JAR. In a Terminal window, use the following commands:

$ wget https://github.com/JodaOrg/joda-time/releases/download/v2.9.4/joda-time-2.9.4-dist.tar.gz
$ tar xvf joda-time-2.9.4-dist.tar.gz

Now, you are ready to start the REPL. Attach the Joda library to its running instance, and import and use the classes it provides, as follows:

$ kotlinc-jvm -cp joda-time-2.9.4/joda-time-2.9.4.jar
Welcome to Kotlin version 1.1-M04 (JRE 1.8.0_66-internal-b17)
Type :help for help, :quit for quit
>>> import org.joda.time.DateTime
>>> DateTime.now()
2016-08-25T22:53:41.017+01:00

Running the preceding code will execute the now function of the DateTime class provided by the Joda library. The output is simply the current date and time.

主站蜘蛛池模板: 徐闻县| 莱芜市| 沂南县| 九江县| 河曲县| 太保市| 嵩明县| 兴国县| 南宫市| 静宁县| 县级市| 天长市| 玛纳斯县| 广南县| 阳谷县| 芮城县| 北海市| 梁平县| 乌兰浩特市| 富裕县| 嘉黎县| 鹿泉市| 闸北区| 双柏县| 凤翔县| 原平市| 平遥县| 北安市| 灵宝市| 四会市| 阳信县| 鞍山市| 龙里县| 威宁| 根河市| 临安市| 江阴市| 自贡市| 苗栗县| 铁岭市| 建昌县|