Add runtime asserts to caveats post

Signed-off-by: Harsh Shandilya <msfjarvis@gmail.com>
This commit is contained in:
Harsh Shandilya 2019-12-12 14:14:31 +05:30
parent 414505ebc4
commit c28d3e33b6
1 changed files with 7 additions and 1 deletions

View File

@ -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 >}}
{{< 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: [Kotlins Assert Is Not Like Javas 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.