Am I missing something or does kotlin not have an ...
# random
r
Am I missing something or does kotlin not have an
identity
lambda out of the box?
a
{ it }
is shorter 😛
Istr the answer is “no, it doesn’t”
m
I would add that it doesn’t need to because Kotlin syntax is already short, as opposed to Java’s where
identity()
is a handy replacement for its lengthy Function type and lambda declarations.
r
but why create the same function over and over again when it could be shared among all places requiring an identity
g
{ it } more self explanatory for me (maybe it’s not true for a person with heavy functional background) Also it a lot easier to modify in future: { it + 1 }
But I really don’t have strong opinion about it, but adding this to stdlib looks unnecessary for me
m
Sorry Robert, let me explain better: my opinion is that there was no need to add it to the standard library due to its short syntax. Anyway if you want, no one is stopping you from defining an
identity
top level function for clarity. If you’re concerned about memory allocation, rest assured that a lambda expression that doesn’t close over parent scope is reused as a singleton: https://stackoverflow.com/questions/42271208/kotlin-safe-lambdas-no-memory-leak/42275638#42275638
r
the question is if it is re-used for this single place or over all places where you have
{ it }
. I guess the first is true
otherwise 👍 🎉
m
Please read the SO link: the latter is true.
g
re-used for this single place or over all places where you have 
{ it }
It reused only in a single class file
m
Isn’t it encoded and allocated once for JVM execution, like String interning? Andrey, where did you get the information about the single file reuse? I’m a little confused now…
g
Kotlin will generate singleton class lambda, it would be reused on runtime
Not sure that it similar how string interning works
m
That’s what I thought, it would generate a singleton that will be reused everywhere the same (non-closure) expression appears, right?
g
but it will not be reused everywhere, only for particular code
this singleton wouldn’t be shared across all class files
it would destroy incremental compilation for example
Combining similar lambdas is what for example Android d8 does, but they do not care so much about incrementality, because Dex file includes all the code, and deduplications of lambdas makes sense there
m
Ok good to know! Thanks for the detailed information
g
you can check any simple Kotlin file with real lambda (not inlined), class would be created for every lambda usage, but different kind of class with different usage depening on is it closed or not
Back to original queston. One more point about { it }, don’t forget that in case of inline lambdas this lambda doesn’t exist, it would be inlined, so usage of cached lambda as idenitiy, would be probably a bit less performant, because instead of inline value directly, idenitiy instance will be invoked with value as argument. Not a practical problem of course, rather theoretical,, but still doesn’t make cached lambda better in all cases And inline lambdas used for most of collection/array operators in kotlin stdlib
r
that's true