Skip to content

Kotlin से Ruby

Recorder: polyglot Kotlin adapter (kotlinc + Java driver)। Top-level functions record किए जा सकते हैं। Ktor और Spring (Kotlin) routes को analyze ढूँढ लेता है; Spring के लिए java.md पढ़ें।

Constructs

  • Null safety: String? -> nil हो सकता है; ?. -> &.; ?: -> || (जब false कोई value न हो) या .nil? ? d : x; !! -> NoMethodError जैसे errors सिर्फ़ वहीं raise करें जहाँ कोई recorded case उन्हें दिखाए।
  • Errors: require(cond) { msg } -> raise ArgumentError, msg unless cond; check() -> RuntimeError (IllegalStateException); error(msg) -> raise msg।
  • Data classes -> Data.define (value equality, copy के लिए with)।
  • Sealed classes / when -> case ... in pattern match या class पर case।
  • Collections: map/filter/fold/groupBy/associateBy/sumOf/joinToString -> map/select/reduce/group_by/index_by/sum/join। listOf immutable है: अगर mutation एक bug होगा तो freeze करें।
  • Ktor: get("/x/{id}") { call.respond(HttpStatusCode.OK, x) } -> route + render json:।

Traps

  • Java वाले ही numeric traps: Int overflow wrap होता है, / zero की ओर truncate करता है, % dividend का sign लेता है (remainder), roundToInt() half up, +infinity की ओर round करता है (2.5 -> 3, -2.5 -> -2)।
  • String.length UTF-16 units गिनता है; जहाँ Ruby nil लौटाता है, वहाँ substring StringIndexOutOfBoundsException throw करता है (-> IndexError)।
  • String पर toInt() NumberFormatException raise करता है (-> ArgumentError, Integer(s) की तरह); toIntOrNull() -> Integer(s, exception: false)।
इस page को GitHub पर edit करें →