Strings

  • 정규식을 선언할때도 큰따옴표 3개를 사용한다 """\d{2}"""

Null 다루기

  • let 구문과 옵셔널 체이닝(?.)을 조합하여, null이 아닐때만 괄호 안의 구문이 실행되도록 할 수 있다.
      message?.let { mailer.sendMessage(email, message)} // message를 it으로도 가능
    

Smart casts

  • 어떤 타입인지 체크할 경우 알아서 해당 타입에 대해 캐스팅이 된다. 링크

      when (expr) {
          is Num -> expr.value
          is Sum -> eval(expr.left) + eval(expr.right)
          else -> throw IllegalArgumentException("Unknown expression")
      }
    
      // ... 중략
      interface Expr
      class Num(val value: Int) : Expr
      class Sum(val left: Expr, val right: Expr) : Expr
    

sealed 키워드

  • sealed로 정의될 경우, 다른 패키지나 모듈에서 접근할 수 없다. 패턴 매칭/when 등에서 유용하게 사용할 수 있다. 링크

      sealed interface Expr
    
      class Num(val value: Int) : Expr
      class Sum(val left: Expr, val right: Expr) : Expr
    

Associate

  • Map 형태의 자료구조를 만들 때 유용하게 사용한다. 링크

      val list = listOf("abc", "cdef")
    
      list.associateBy { it.first() } // == mapOf('a' to "abc", 'c' to "cdef")
    
      list.associateWith { it.length } // == mapOf("abc" to 3, "cdef" to 4)
    
      list.associate { it.first() to it.length } // == mapOf('a' to 3, 'c' to 4)
    

Fold And Reduce

  • 링크
    • 초기값을 지정하고 싶을때는 Fold를 사용한다.
    • 모든 축소 작업은 빈 컬렉션일때는 Exception을 발생한다. 이에 대응하려면 orNull을 사용하면 null로 받을 수 있다.