When to use inline functions in Kotlin and how do they affect performance?
Hi everyone! I recently started learning Kotlin and came across something called inline functions. The documentation says they help avoid the overhead of creating lambda objects, but I don't fully understand when they should actually be used.\n\nFor example, I have a simple higher-order function:\n\n```kotlin\nfun measureTime(action: () -> Unit) {\n val start = System.currentTimeMillis()\n action()\n val end = System.currentTimeMillis()\n println("Time: ${end - start} ms")\n}\n```\n\nI call it like this:\n\n```kotlin\nmeasureTime {\n Thread.sleep(100)\n println("Hello")\n}\n```\n\nIt works fine, but I read that without `inline`, a new object is created for the lambda every time. Is this really a problem? When does it become critical?\n\nAlso, I've seen examples with `reified`
No answers yet.
Log in to leave an answer
Blogs