https://kotlinlang.org logo
Title
t

Travis Griggs

03/24/2020, 5:55 PM
I’m trying to get
assert()
to do something meaningful. I note that I’m supposed to run the JVM with
-ea
(as per https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/assert.html). But where do I actually do that at?
j

jw

03/24/2020, 6:19 PM
All Android processes are forked from the Zygote which has assertions disabled. Thus, it's impossible to use on Android unless you create your own VM process (which you can only do as the system or from CLI).
👍 1
t

Travis Griggs

03/24/2020, 6:39 PM
dang. ok. is there an “assert like” functionality for development? I don’t need it to be overheadless
I just copy/pasted/tweaked the kotlin.assert() function:
public inline fun affirm(value: Boolean, lazyMessage: () -> String = {""}) {
   if (!value) {
      val message = lazyMessage()
      throw AssertionError(message)
   }
}
works for for my needs
s

StavFX

03/24/2020, 7:38 PM
check
and
require
will throw
IllegalStateException
and
IllegalArgumentException
So I’d use them unless you specifically need AssertionError to be thrown. However, they’ll throw regardless of the JVM args (non development builds)
☝️ 1
t

trevjones

03/24/2020, 8:45 PM
AGP 4.1a4 has a version of D8 that I assume rewrites java asserts in debug builds to a typical
if condition throw
. Hopefully we see the kotlin assert support added in short order.
j

jw

03/24/2020, 11:10 PM
wait, really? is there a bug for that? is that only in debug builds? previously it would throw away the assert and any nodes flowing into it
j

jw

03/24/2020, 11:22 PM
Thank you. Glad to see it's debug only. I had only seen the beginnings of this conversation, didn't realize it had shipped!
t

trevjones

03/24/2020, 11:23 PM
now to just convince people the value of runtime invariant enforcement
j

jw

03/25/2020, 2:27 PM
Wrote a blog post about this. Thanks again for the tip that it shipped! https://jakewharton.com/d8-optimization-assertions/
💯 1
s

StavFX

03/25/2020, 5:48 PM
Well that was quick 🙂
t

trevjones

03/25/2020, 5:49 PM
feels like it was previously drafted and nearly ready to go 😆 . if so, I wonder how long that list of drafts is.
😂 1
j

jw

03/25/2020, 7:06 PM
Wrote it all last night with some good beer. I have a list of topics only. That one was farther down the queue.
👏 3