Kotlin Cookbook 1

JinHyeong bak
3 min readFeb 22, 2022

--

예제 코드: https://github.com/kousen/kotlin-cookbook

1. https://play.kotlinlang.org/

2. https://kotlinlang.org/docs/command-line.html#homebrew

Homebrew

Alternatively, on OS X you can install the compiler via Homebrew.

$ brew update
$ brew install kotlin

동작 확인:


$ kotlin -version
Kotlin version 1.6.10-release-923 (JRE 17.0.2+0)

3. kotlinc-jvm hello.kt

$ kotlinc-jvm hello.kt
$ tree
.
├── HelloKt.class
├── META-INF
│ └── main.kotlin_module
└── hello.kt
$ kotlin HelloKt
Hello, Kotlin

코틀린은 자바 소스 코드를 생성하지 않는다. 코틀린 컴파일러는 JVM이 해석할 수 있는 바이트코드를 생성한다.

$ kotlinc-jvm hello.kt -include-runtime -d hello.jar$ tree
.
├── hello.jar
└── hello.kt
0 directories, 2 files

$ java -jar hello.jar
Hello, Kotlin
$ jar -xvf hello.jar
$ tree
.
├── HelloKt.class
├── META-INF
│ ├── MANIFEST.MF
│ ├── main.kotlin_module
│ ├── services
│ │ ├── kotlin.reflect.jvm.internal.impl.builtins.BuiltInsLoader
│ │ └── kotlin.reflect.jvm.internal.impl.resolve.ExternalOverridabilityCondition
│ └── versions
│ └── 9
│ └── kotlin
│ └── reflect
│ └── jvm
│ └── internal
│ └── impl
│ └── serialization
│ └── deserialization
│ └── builtins
│ └── BuiltInsResourceLoader.class
├── hello.jar
├── hello.kt
└── kotlin
├── ArrayIntrinsicsKt.class
├── BuilderInference.class
├── CharCodeJVMKt.class
├── CharCodeKt.class
├── CompareToKt.class
├── DeepRecursiveFunction.class
├── DeepRecursiveKt.class
├── DeepRecursiveScope.class
├── DeepRecursiveScopeImpl$crossFunctionCompletion$$inlined$Continuation$1.class
├── DeepRecursiveScopeImpl.class
├── Deprecated.class
├── DeprecatedSinceKotlin.class
├── DeprecationLevel.class
├── DslMarker.class
├── ExceptionsKt.class
...

4. kotlinc

Read-Eval-Print Loop

kotlinc
Welcome to Kotlin version 1.6.10 (JRE 17.0.2+0)
Type :help for help, :quit for quit
>>> println("hello")
hello
>>> var name = "Dol"
>>> println("Hello, $name!")
Hello, Dol!
>>> :help
Available commands:
:help show this help
:quit exit the interpreter
:dump bytecode dump classes to terminal
:load <file> load script from specified file
>>>

5. kotlinc -script southpole.kts

$ kotlinc -script southpole.kts
It is 01:57:09.649647 (UTC+13:00) at the South Pole
The South Pole is on Daylight Savings Time
$ tree
.
└── southpole.kts
0 directories, 1 file

코틀린은 JVM상에서 스크립트 언어로도 사용된다.

6. 실행속도 빠르기 순위: 1. 네이티브 이미지, 2. JAR파일, 3. 직접 실행

7. 그레이들는 젯브레인사가 제공하는 플러그인을 사용해서 JVM 코틀린 컴파일을 지원한다.

8. 그루비 DSL과 큰 차이점은

모든 문자열에 큰따옴표를 사용.
코틀린 DSL에서는 괄호 사용이 필수.
코틀린 콜론 대신 등호 기호(=)를 사용해 값을 할당.

9. 자바 라이브러리 플러그인(`java-library`)에는 JVM을 기반으로 하는 프로젝트를 위한 build, compileJava, compileTestJava, javadoc, jar 와 같은 그레이들 작업이 정의되어 있다.

Java 라이브러리 플러그인은 Java 라이브러리에 대한 특정 지식을 제공하여 Java 플러그인의 기능을 확장합니다. 특히 Java 라이브러리는 API를 소비자(즉, Java 또는 Java 라이브러리 플러그인을 사용하는 다른 프로젝트)에 노출합니다. Java 플러그인에 의해 노출된 모든 소스 세트, 작업 및 구성은 이 플러그인을 사용할 때 암시적으로 사용할 수 있습니다.

뭔소리여? 일단 패스.

$ ./gradlew build -mWelcome to Gradle 7.1!Here are the highlights of this release:
- Faster incremental Java compilation
- Easier source set configuration in the Kotlin DSL
For more details see https://docs.gradle.org/7.1/release-notes.htmlStarting a Gradle Daemon, 2 incompatible Daemons could not be reused, use --status for details
:compileKotlin SKIPPED
:compileJava SKIPPED
:processResources SKIPPED
:classes SKIPPED
:inspectClassesForKotlinIC SKIPPED
:jar SKIPPED
:assemble SKIPPED
:compileTestKotlin SKIPPED
:compileTestJava SKIPPED
:processTestResources SKIPPED
:testClasses SKIPPED
:test SKIPPED
:check SKIPPED
:build SKIPPED
BUILD SUCCESSFUL in 6s

10. 프로젝트에 코틀린과 자바 코드가 둘 다 있다면 코트린 컴파일러가 먼저 호출되어야 한다.

kotlin-maven-plugin이 먼저 실행되어야 한다.

--

--