How to exploit kotlin's inline benefits when function has an object with a lambda parameter


How to exploit kotlin's inline benefits when function has an object with a lambda parameter



lets say I have a data class like so


private data class StubModel<T>(
val invocation: ItemsFactory.() -> T?,
val returnValue: T
)



and I have a function like so


private fun <T> stubIt(stubs: List<StubModel<T>>) {
for (stub in stubs) {
whenever(stub.invocation()).thenReturn(stub.returnValue)
}
}



now when I try to the inline the stubIt function I dont get any performance benefits (i.e the lambda parameter in the StubModel is not inlined which is why I want the power of inline), how do I get kotlin compiler to inline the lambda parameter in the model class at the call site?


stubIt


StubModel




1 Answer
1



This is not possible. As a property of your StubModel, the invocation needs to be an object instance and stored as such. This is just, what inline functions are meant for to prevent. Also having a list as a parameter makes it impossible to inline anything, because how should the compiler know about the values at runtime.


StubModel


invocation






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

PySpark - SparkContext: Error initializing SparkContext File does not exist

List of Kim Possible characters

Python Tkinter Error, “Too Early to Create Image”