이디엄(Idioms , 관용구)
DTO 작성 (POJO / POCO)
data class Customer(val name: String, val email: String)
기본 제공하는 기능
- 모든 프로퍼티의 getter (var의 경우는 setter)
- equals()
- hasCode()
- toString()
- copy()
- component1(), component2() ... 모든 프로퍼티 (데이터 클래스 참고)
함수 파라미터 기본값
fun foo(a: Int = 0, b: String = "") { ... }
리스트 필터링
val positives = list.filter { x -> x > 0 }
좀 더 축약하면
val positives = list.filter { it > 0 }
문자열 보간
println("Name $name")
인스턴스 체크
when (x) {
    is Foo -> ...
    is Bar -> ...
    else   -> ...
}
map/list pair 탐색
for ((k, v) in map) {
    println("$k -> $v")
}
범위 사용
for (i in 1..100) { ... }          // 100 포함
for (i in 1 until 100) { ... }     // 100 미포함
for (x in 2..10 step 2) { ... }
for (x in 10 downTo 1) { ... }
if (x in 1..10) { ... }
읽기전용 리스트
val list = listOf("a", "b", "c")
읽기전용 맵
val map = mapOf("a" to 1, "b" to 2, "c" to 3)
lazy (지연) 프로퍼티
val p: String by lazy {
    // compute the string
}
확장기능
fun String.spaceToCamelCase() { ... }
"Convert this to camelcase".spaceToCamelCase()
싱글톤 작성
object Resource {
    val name = "Name"
}
null이 아닌 경우 생략
val files = File("Test").listFiles()
println(files?.size)
println(files?.size ?: "empty")
null이 아닌 경우 실행
val data = ...
data?.let {
    ... // null이 아닌 경우 블록을 실행
}
return 문
fun transform(color: String): Int {
    return when (color) {
        "Red" -> 0
        "Green" -> 1
        "Blue" -> 2
        else -> throw IllegalArgumentException("Invalid color param value")
    }
}
try/catch
fun test() {
    val result = try {
        count()
    } catch (e: ArithmeticException) {
        throw IllegalStateException(e)
    }
    // result 작업
}
if 문
fun foo(param: Int) {
    val result = if (param == 1) {
        "one"
    } else if (param == 2) {
        "two"
    } else {
        "three"
    }
}
Unit을 반환하는 메서를 Builder 스타일로 사용
fun arrayOfMinusOnes(size: Int): IntArray {
    return IntArray(size).apply { fill(-1) }
}
단일식 함수
fun theAnswer() = 42
// fun theAnswer(): Int {
//     return 42
// }
fun transform(color: String): Int = when (color) {
    "Red" -> 0
    "Green" -> 1
    "Blue" -> 2
    else -> throw IllegalArgumentException("Invalid color param value")
}
객체 인스턴스의 복수 메서드 호출 (with)
class Turtle {
    fun penDown()
    fun penUp()
    fun turn(degrees: Double)
    fun forward(pixels: Double)
}
val myTurtle = Turtle()
with(myTurtle) { //draw a 100 pix square
    penDown()
    for(i in 1..4) {
        forward(100.0)
        turn(90.0)
    }
    penUp()
}
Java7의 try with resouces
val stream = Files.newInputStream(Paths.get("/some/file.txt"))
stream.buffered().reader().use { reader ->
    println(reader.readText())
}
제네릭 타입 정보가 필요한 제네릭 함수를 위한 간편한 폼
//  public final class Gson {
//     ...
//     public <T> T fromJson(JsonElement json, Class<T> classOfT) throws JsonSyntaxException {
//     ...
inline fun <reified T: Any> Gson.fromJson(json): T = this.fromJson(json, T::class.java)
null 가능한 Boolean 타입 사용
val b: Boolean? = ...
if (b == true) {
    ...
} else {
    // `b` 는 false 혹은 null
}