# \[SOLVED\] Issues with serializable lambdas and SAM interfaces

**URL:** <https://discuss.kotlinlang.org/t/solved-issues-with-serializable-lambdas-and-sam-interfaces/19969>\
**Category:** Support\
**Created:** [November 20, 2020, 12:00pm UTC](https://discuss.kotlinlang.org/t/solved-issues-with-serializable-lambdas-and-sam-interfaces/19969 "2020-11-20T12:00:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![JoonasC](https://avatars.discourse-cdn.com/v4/letter/j/da6949/32.png) [@JoonasC](https://discuss.kotlinlang.org/u/JoonasC)\
**Post date:** [November 20, 2020, 12:00pm UTC](https://discuss.kotlinlang.org/t/solved-issues-with-serializable-lambdas-and-sam-interfaces/19969/1 "2020-11-20T12:00:12Z")

</div>

I’m trying to use a java library for distributed background job processing called JobRunr ([https://www.jobrunr.io/en/](https://www.jobrunr.io/en/)) from Kotlin.  
JobRunr works by serializing a lambda containing the code that you want to run in your background job into a database (eg. Redis), where other servers running JobRunr can pick up the lambda, deserialize it and execute it.  
JobRunr achieves this with a SAM (Functional) interface, that implements serializable.  
It seems that Kotlin does not compile serializable SAM lambdas in the same way as Java does and that causes problems.  
Here is the link to an issue I opened on JobRunr’s GitHub about the same problem: [https://github.com/jobrunr/jobrunr/issues/63](https://github.com/jobrunr/jobrunr/issues/63).

Does this issue have a quick and dirty workaround, or do I need to open a YouTrack ticket for this?

Thanks.

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [November 20, 2020, 12:27pm UTC](https://discuss.kotlinlang.org/t/solved-issues-with-serializable-lambdas-and-sam-interfaces/19969/2 "2020-11-20T12:27:37Z")

</div>

The issue AFAICT is that kotlin lambdas aren’t serializable. The fact that it extends `Serializable` means nothing since that interface is cosmetic at best in java/JVM. You would need to figure out a separate way to serialize kotlin lambdas.  
It looks like your code depends on the way java implements serialization for lambdas. For this reason you also prevent using any “normal” implementation of your functional interface.  
Maybe you could use reflection to serialize kotlin lambdas. Not sure how to do it exactly though.

> [@JoonasC](#):
>
> Does this issue have a quick and dirty workaround, or do I need to open a YouTrack ticket for this?

You could try opening a ticket but I seem to remember that the desicion to make lambdas not serializable is as designed. That doesn’t mean it can’t be changed. Also it wouldn’t surprise me if there is a ticket for this already.

---

<div class="post-metadata">

**Author:** ![JoonasC](https://avatars.discourse-cdn.com/v4/letter/j/da6949/32.png) [@JoonasC](https://discuss.kotlinlang.org/u/JoonasC)\
**Post date:** [November 20, 2020, 2:03pm UTC](https://discuss.kotlinlang.org/t/solved-issues-with-serializable-lambdas-and-sam-interfaces/19969/3 "2020-11-20T14:03:42Z")

</div>

@Wasabi375  
Hi,  
Do you think I could maybe work around the issue by using an object, and manually implementing the functions, that the java serialized lambda implements, such as:

```auto
jobScheduler.enqueue(object: JobLambda {
    override fun run() {
        println("Hello")
    }

    fun writeReplace() {
        TODO("Implementation here")
    }
})

```

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [November 20, 2020, 2:51pm UTC](https://discuss.kotlinlang.org/t/solved-issues-with-serializable-lambdas-and-sam-interfaces/19969/4 "2020-11-20T14:51:16Z")

</div>

I think that should work.

---

<div class="post-metadata">

**Author:** ![JoonasC](https://avatars.discourse-cdn.com/v4/letter/j/da6949/32.png) [@JoonasC](https://discuss.kotlinlang.org/u/JoonasC)\
**Post date:** [November 20, 2020, 3:42pm UTC](https://discuss.kotlinlang.org/t/solved-issues-with-serializable-lambdas-and-sam-interfaces/19969/5 "2020-11-20T15:42:15Z")

</div>

I ended up just using a Java wrapper method:

```auto
public void helloWorldJob(JobScheduler jobScheduler) {
    jobScheduler.enqueue(() -> System.out.println("Hello"));
}

```
