# How to create a SerializedLambda

**URL:** <https://discuss.kotlinlang.org/t/how-to-create-a-serializedlambda/8502>\
**Category:** Support\
**Created:** [July 9, 2018, 10:59pm UTC](https://discuss.kotlinlang.org/t/how-to-create-a-serializedlambda/8502 "2018-07-09T22:59:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![icr00](https://avatars.discourse-cdn.com/v4/letter/i/3ec8ea/32.png) [@icr00](https://discuss.kotlinlang.org/u/icr00)\
**Post date:** [July 9, 2018, 10:59pm UTC](https://discuss.kotlinlang.org/t/how-to-create-a-serializedlambda/8502/1 "2018-07-09T22:59:08Z")

</div>

Hi all,  
So I was wondering if it was possible to create a SerializedLambda from a Kotlin lambda?  
The SerializedLambda constructor looks quite off-putting!  
Any advice on this?  
Thanks!

---

<div class="post-metadata">

**Author:** ![icr00](https://avatars.discourse-cdn.com/v4/letter/i/3ec8ea/32.png) [@icr00](https://discuss.kotlinlang.org/u/icr00)\
**Post date:** [July 10, 2018, 9:40am UTC](https://discuss.kotlinlang.org/t/how-to-create-a-serializedlambda/8502/2 "2018-07-10T09:40:02Z")

</div>

Ok, so going a bit deeper… do Kotlin lambdas serialize into the same format as a Java lambda?

So, even if the class name is different, would the serial byte data be the same as / or compatible with Java?

---

<div class="post-metadata">

**Author:** ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)\
**Post date:** [July 10, 2018, 12:38pm UTC](https://discuss.kotlinlang.org/t/how-to-create-a-serializedlambda/8502/3 "2018-07-10T12:38:05Z")

</div>

Currently Kotlin lambda functions are converted to Java anonymous classes, so they have nothing to do with Java 8 lambda functions. Kotlin also does not use invokedynamica at the moment.

---

<div class="post-metadata">

**Author:** ![icr00](https://avatars.discourse-cdn.com/v4/letter/i/3ec8ea/32.png) [@icr00](https://discuss.kotlinlang.org/u/icr00)\
**Post date:** [July 12, 2018, 10:24pm UTC](https://discuss.kotlinlang.org/t/how-to-create-a-serializedlambda/8502/4 "2018-07-12T22:24:43Z")

</div>

Ok, sounds like this is a no go then.  
Is this a slight shortcoming in kotlin I wonder?  
JINQ uses an interpretation of the serialised lambda bytecode to generate dynamic SQL queries (like MS LINQ, although I suspect a different mechanism). There doesn’t seem to be any way to do this (interrogate the structure of an expression) in Kotlin.  
I wonder if there is anything planned…?

---

<div class="post-metadata">

**Author:** ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)\
**Post date:** [July 13, 2018, 6:11am UTC](https://discuss.kotlinlang.org/t/how-to-create-a-serializedlambda/8502/5 "2018-07-13T06:11:24Z")

</div>

I don’t think that basing something on serialized form of lambda is a good solution. It is very unreliable. Kotlin provides much more powerful mechanism called type safe builders, which is very good for things like SQL requests.

As for expression serialization, my group is actually working on a polyglot expression serialization (only for mathematics for now), but the task seems to be very complicated, so we are on the discussion stage.

---

<div class="post-metadata">

**Author:** ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)\
**Post date:** [July 13, 2018, 6:13am UTC](https://discuss.kotlinlang.org/t/how-to-create-a-serializedlambda/8502/6 "2018-07-13T06:13:11Z")

</div>

By the way, here is relevant [StackOverflow question](https://stackoverflow.com/questions/40064479/jinq-in-kotlin-how-to-convert-lambda-into-java-serializedlambda). If you want to use the framework, the simplest way is to write parts of code in Java. It is not hard, really.

---

<div class="post-metadata">

**Author:** ![icr00](https://avatars.discourse-cdn.com/v4/letter/i/3ec8ea/32.png) [@icr00](https://discuss.kotlinlang.org/u/icr00)\
**Post date:** [July 26, 2018, 8:57pm UTC](https://discuss.kotlinlang.org/t/how-to-create-a-serializedlambda/8502/7 "2018-07-26T20:57:49Z")

</div>

Sorry for the slow reply, but yes I agree - type-safe builders are the way to go.
