# Lambda expression is not using invokedynamic

**URL:** <https://discuss.kotlinlang.org/t/lambda-expression-is-not-using-invokedynamic/22834>\
**Category:** Support\
**Created:** [August 27, 2021, 10:43pm UTC](https://discuss.kotlinlang.org/t/lambda-expression-is-not-using-invokedynamic/22834 "2021-08-27T22:43:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ranjith.masthikatte](https://avatars.discourse-cdn.com/v4/letter/r/f14d63/32.png) [@ranjith.masthikatte](https://discuss.kotlinlang.org/u/ranjith.masthikatte)\
**Post date:** [August 27, 2021, 10:43pm UTC](https://discuss.kotlinlang.org/t/lambda-expression-is-not-using-invokedynamic/22834/1 "2021-08-27T22:43:24Z")

</div>

Hi,

I tried lambda expression in Kotlin (1.5.10) with target Java 1.8. I set the compiler option -Xlambdas=indy in maven pom file. But, in Java bytecode, it still uses the wrapper class but not the invokedynamic.

And also documentation states that SAM by default uses invokedynamic (from 1.5). But, I checked, it still creates a wrapper class.

Could you please let me know, where I am going wrong?

Thanks

My Example code

fun interface Vehicle {  
fun start()  
fun stop() {  
println(“Vehicle is stopped”)  
}  
}

fun main() {  
val vehicle = Vehicle { println(“Car is started”) }  
vehicle.start()  
vehicle.stop()  
}

---

<div class="post-metadata">

**Author:** ![kyay10](https://avatars.discourse-cdn.com/v4/letter/k/c57346/32.png) [@kyay10](https://discuss.kotlinlang.org/u/kyay10)\
**Post date:** [August 28, 2021, 4:22pm UTC](https://discuss.kotlinlang.org/t/lambda-expression-is-not-using-invokedynamic/22834/2 "2021-08-28T16:22:41Z")

</div>

~~Try a normal lambda instead of a `fun interface`, because `fun interface`s are compiled as wrapper classes because they specifically have to implement that interface AFAIK, although I think there’s plans to change that.~~  
Edit: `fun interface`s, according to the Kotlin docs, should use indy by default since 1.5.10:

> ### SAM adapters via invokedynamic
> 
> Kotlin 1.5.0 now uses dynamic invocations ( `invokedynamic` ) for compiling SAM (Single Abstract Method) conversions:
> 
> - Over any expression if the SAM type is a [Java interface](https://kotlinlang.org/docs/java-interop.html#sam-conversions)
> - Over lambda if the SAM type is a [Kotlin functional interface](https://kotlinlang.org/docs/fun-interfaces.html#sam-conversions)
> 
> The new implementation uses [`LambdaMetafactory.metafactory()`](https://docs.oracle.com/javase/8/docs/api/java/lang/invoke/LambdaMetafactory.html#metafactory-java.lang.invoke.MethodHandles.Lookup-java.lang.String-java.lang.invoke.MethodType-java.lang.invoke.MethodType-java.lang.invoke.MethodHandle-java.lang.invoke.MethodType-) and auxiliary wrapper classes are no longer generated during compilation

Perhaps check that your Jvm target version is 1.8?

---

<div class="post-metadata">

**Author:** ![ranjith.masthikatte](https://avatars.discourse-cdn.com/v4/letter/r/f14d63/32.png) [@ranjith.masthikatte](https://discuss.kotlinlang.org/u/ranjith.masthikatte)\
**Post date:** [August 28, 2021, 5:40pm UTC](https://discuss.kotlinlang.org/t/lambda-expression-is-not-using-invokedynamic/22834/3 "2021-08-28T17:40:50Z")

</div>

yes, my JVM target is 1.8.

---

<div class="post-metadata">

**Author:** ![ranjith.masthikatte](https://avatars.discourse-cdn.com/v4/letter/r/f14d63/32.png) [@ranjith.masthikatte](https://discuss.kotlinlang.org/u/ranjith.masthikatte)\
**Post date:** [August 30, 2021, 11:31am UTC](https://discuss.kotlinlang.org/t/lambda-expression-is-not-using-invokedynamic/22834/4 "2021-08-30T11:31:48Z")

</div>

I found the problem. My example was not a pure Kotlin functional interface, because it contains two methods one abstract and one default implementation. I tried with the pure Kotlin functional interface and by default, it generates the bytecode with “invokedynamic”. Below is the example. And for the lambda, with just -Xlambda=indy compiler options is enough to generate the bytecode with “invokedynamic” for the lambda expression. (Initially, it did not generate, I am not sure why, but after that, I removed OptIn compiler options, and after I compiled using “mvn clean” command it worked)

fun interface Vehicle {  
fun start()  
}

fun main() {  
val vehicle = Vehicle { println(“Car is started”) }  
vehicle.start()  
}

---

<div class="post-metadata">

**Author:** ![madmax1028](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/madmax1028/32/3335_2.png) [@madmax1028](https://discuss.kotlinlang.org/u/madmax1028)\
**Post date:** [August 30, 2021, 3:02pm UTC](https://discuss.kotlinlang.org/t/lambda-expression-is-not-using-invokedynamic/22834/5 "2021-08-30T15:02:49Z")

</div>

> [@ranjith.masthikatte](#):
>
> fun interface Vehicle {  
> fun start()  
> fun stop() {  
> println(“Vehicle is stopped”)  
> }  
> }
> 
> fun main() {  
> val vehicle = Vehicle { println(“Car is started”) }  
> vehicle.start()  
> vehicle.stop()  
> }

Have you also added `-Xjvm-default=all` flag? It’s required if you want to have `fun interface`s treated as such also from the Java perspective.

---

<div class="post-metadata">

**Author:** ![ranjith.masthikatte](https://avatars.discourse-cdn.com/v4/letter/r/f14d63/32.png) [@ranjith.masthikatte](https://discuss.kotlinlang.org/u/ranjith.masthikatte)\
**Post date:** [August 30, 2021, 3:28pm UTC](https://discuss.kotlinlang.org/t/lambda-expression-is-not-using-invokedynamic/22834/6 "2021-08-30T15:28:36Z")

</div>

No, I did not add that initially. However, I tried now that option, and it works even for my initial example code. that’s amazing.

Thank you 🙂
