From c28d3e33b6c1d3ecda5d7dd894fa5c31cead9706 Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Thu, 12 Dec 2019 14:14:31 +0530 Subject: [PATCH] Add runtime asserts to caveats post Signed-off-by: Harsh Shandilya --- .../teachingkotlin-part-3--caveats-coming-from-java.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/content/posts/teachingkotlin-part-3--caveats-coming-from-java.md b/content/posts/teachingkotlin-part-3--caveats-coming-from-java.md index a15640d..7baa3cf 100644 --- a/content/posts/teachingkotlin-part-3--caveats-coming-from-java.md +++ b/content/posts/teachingkotlin-part-3--caveats-coming-from-java.md @@ -14,4 +14,10 @@ When you start migrating your Java code to Kotlin, you will encounter multiple s Java's `java.lang.String#split` [method](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#split-java.lang.String-) takes a `String` as it's first argument and creates a `Regex` out of it before attempting to split. Kotlin, however, has two variants of this method. One takes a `String` and uses it as a plaintext delimiter, and the other takes a `Regex` behaving like the Java method we mentioned earlier. Code that was directly converted from Java to Kotlin will fail to accommodate this difference, so be on the lookout. -{{< tweet 1202077283579826176 >}} \ No newline at end of file +{{< tweet 1202077283579826176 >}} + +## Runtime asserts + +Square's [Jesse Wilson](https://github.com/swankjesse) found through an [OkHttp](https://github.com/square/okhttp) bug that Kotlin's `assert` function differs from Java's in a very critical way - the asserted expression is *always* executed. He's written about it on his blog which you can check out for a proper write up: [Kotlin’s Assert Is Not Like Java’s Assert](https://publicobject.com/2019/11/18/kotlins-assert-is-not-like-javas-assert/). + +TL;DR Java's `assert` checks the `java.lang.Class#desiredAssertionStatus` method **before** executing the expression, but Kotlin does it **after** which results in unnecessary, potentially significant overhead.