Skip to content

Kotlin에서 Ruby로

Recorder: polyglot Kotlin 어댑터(kotlinc + Java 드라이버). 최상위 함수를 기록할 수 있습니다. Ktor와 Spring(Kotlin) 라우트는 analyze가 찾아냅니다. Spring은 java.md를 참고하세요.

구문

  • 널 안전성: String? -> nil일 수 있음, ?. -> &., ?: -> ||(false가 유효한 값이 아닐 때) 또는 .nil? ? d : x, !! -> 기록된 케이스에 나타날 때만 NoMethodError 같은 에러를 발생.
  • 에러: require(cond) { msg } -> raise ArgumentError, msg unless cond, check() -> RuntimeError(IllegalStateException), error(msg) -> raise msg.
  • Data class -> Data.define(값 동등성, copy는 with).
  • Sealed class / when -> case ... in 패턴 매칭 또는 클래스에 대한 case.
  • 컬렉션: map/filter/fold/groupBy/associateBy/sumOf/joinToString -> map/select/reduce/group_by/index_by/sum/join. listOf는 불변입니다. 변경하는 것이 버그라면 freeze하세요.
  • Ktor: get("/x/{id}") { call.respond(HttpStatusCode.OK, x) } -> 라우트 + render json:.

함정

  • 숫자 관련 함정은 Java와 같습니다. Int 오버플로는 순환하고, /는 0 쪽으로 버리며, %는 피제수의 부호를 따르고(remainder), roundToInt()는 0.5를 +무한대 쪽으로 올립니다(2.5 -> 3, -2.5 -> -2).
  • String.length는 UTF-16 유닛을 셉니다. substring은 Ruby라면 nil을 반환할 곳에서 StringIndexOutOfBoundsException(-> IndexError)을 던집니다.
  • 문자열에 toInt()를 쓰면 NumberFormatException(-> Integer(s)처럼 ArgumentError)이 발생합니다. toIntOrNull() -> Integer(s, exception: false).
GitHub에서 이 페이지 수정하기 →