given this function ```inline fun <T> doNoth...
# getting-started
y
given this function
Copy code
inline fun <T> doNothing(ignored: T) { /** empty */ }
I think it's reasonable to expect the optimizer to get rid of it. is it also reasonable to have the same expectations for this somewhat more useful version?
Copy code
inline fun doNothing(vararg ignored: Any?) { }
y
Not necessarily sure about the Kotlin compiler itself, but if you use something like Proguard or R8, it will definitely eliminate it completely
c
could validate current behaviour by inspecting compiler output (bytecode for JVM, etc), though that would be subject to change and perhaps dependent on other conditions.
e
Kotlin generates a non-inline version of
inline fun
, in addition to inlining it at callsites. (for reflection purposes afaik)
y
@ephemient could you elaborate on the implications of this?
e
there will exist a
doNothing
function in the bytecode, even though it won't be directly referenced (at least, not from Kotlin code)
also IIRC the kotlin compiler isn't able to optimize away the creation of the varargs array in the second case
y
I see. I'm unfamiliar with JVM - can the first version having a generic parameter, lead to the kind of bloat that is typically associated with generics in other languages?
e
no. generics are erased
c
this is distinctly different than say C++, in which generics are largely a templating mechanism, repeating the code (with different types filled in_.
e
the out-of-line version is never reified (and will contain an assertion instead, rendering it inoperable if called via reflection which cannot supply a reified type parameter)
y
thanks a lot to everyone who answered
Kotlin ~ Java, as far as the object model goes
(Swift isn't explicitly mentioned on that table, but it's kind of a mix, monomorphizing data structures while passing around type witnesses so that generic functions only need to be produced once. similar to Scala but hidden)