# Creating a listener without an interface?

**URL:** <https://discuss.kotlinlang.org/t/creating-a-listener-without-an-interface/22388>\
**Category:** Android\
**Created:** [July 12, 2021, 6:36am UTC](https://discuss.kotlinlang.org/t/creating-a-listener-without-an-interface/22388 "2021-07-12T06:36:13Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![artan.berisha](https://avatars.discourse-cdn.com/v4/letter/a/eada6e/32.png) [@artan.berisha](https://discuss.kotlinlang.org/u/artan.berisha)\
**Post date:** [July 12, 2021, 6:36am UTC](https://discuss.kotlinlang.org/t/creating-a-listener-without-an-interface/22388/1 "2021-07-12T06:36:13Z")

</div>

I came across this custom onClickListener that handles clicks(in one of Udacity courses), I come from java world so even that I know what this does, I don’t understand this syntax very well. I know that is getting a function as parameter( a lambda ) but how it gets the behavior of an interface I don’t know 😕 . Can someone help me understand ? (I am trying to convert it to java first and then understand it more why they did it this way)

```auto
class OnClickListener(val clickListener: (marsProperty:MarsProperty) -> Unit) {
    fun onClick(marsProperty:MarsProperty) = clickListener(marsProperty)
}

```

---

<div class="post-metadata">

**Author:** ![broot](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@broot](https://discuss.kotlinlang.org/u/broot)\
**Post date:** [July 12, 2021, 7:03am UTC](https://discuss.kotlinlang.org/t/creating-a-listener-without-an-interface/22388/2 "2021-07-12T07:03:21Z")

</div>

What do you mean by saying that “it gets the behavior of an interface”? Functions in Kotlin are first-class citizens, so you can pass functions as any other data objects and invoke them if needed.

You shouldn’t assume that you will be able to convert any possible Kotlin code 1:1 to Java, but in this case probably the best analogy would be to replace `(marsProperty:MarsProperty) -> Unit` with: `Consumer<MarsProperty>`:

```auto
public class OnClickListener {
    private Consumer<MarsProperty> clickListener;

    public OnClickListener(Consumer<MarsProperty> clickListener) {
        this.clickListener = clickListener;
    }

    public void onClick(MarsProperty marsProperty) {
        clickListener.accept(marsProperty);
    }
}

```

If you didn’t read it yet, it is explained in official docs: [High-order functions and lambdas | Kotlin](https://kotlinlang.org/docs/lambdas.html)

---

<div class="post-metadata">

**Author:** ![chipays](https://avatars.discourse-cdn.com/v4/letter/c/ad7895/32.png) [@chipays](https://discuss.kotlinlang.org/u/chipays)\
**Post date:** [July 12, 2021, 1:09pm UTC](https://discuss.kotlinlang.org/t/creating-a-listener-without-an-interface/22388/3 "2021-07-12T13:09:54Z")

</div>

`(marsProperty:MarsProperty) -> Unit` would, in the java world, be a [Function\<MarsProperty,Unit\>](https://docs.oracle.com/javase/8/docs/api/java/util/function/Function.html).

`() -> Unit` would be a [Supplier](https://docs.oracle.com/javase/8/docs/api/java/util/function/Supplier.html).

`(String, Int) -> Double` would be a [BiFunction\<String,Int,Double\>](https://docs.oracle.com/javase/8/docs/api/java/util/function/BiFunction.html).

The difference is that Kotlin hides what the actual implementing is called behind a syntactical construct.

---

<div class="post-metadata">

**Author:** ![artan.berisha](https://avatars.discourse-cdn.com/v4/letter/a/eada6e/32.png) [@artan.berisha](https://discuss.kotlinlang.org/u/artan.berisha)\
**Post date:** [July 12, 2021, 1:53pm UTC](https://discuss.kotlinlang.org/t/creating-a-listener-without-an-interface/22388/4 "2021-07-12T13:53:42Z")

</div>

I think I wasn’t clear enough(I know the concept of lambdas), what I meant is that in Java(Android) I always used interfaces to implement callbacks, and now when I saw a callback implemented without an interface I got confused, but I guess that this is just another way in Kotlin to implement callbacks right ?

---

<div class="post-metadata">

**Author:** ![cylab](https://avatars.discourse-cdn.com/v4/letter/c/b5ac83/32.png) [@cylab](https://discuss.kotlinlang.org/u/cylab)\
**Post date:** [July 12, 2021, 2:17pm UTC](https://discuss.kotlinlang.org/t/creating-a-listener-without-an-interface/22388/5 "2021-07-12T14:17:44Z")

</div>

The term you are searching for is SAM conversion: [Functional (SAM) interfaces | Kotlin](https://kotlinlang.org/docs/fun-interfaces.html#sam-conversions)

---

<div class="post-metadata">

**Author:** ![broot](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@broot](https://discuss.kotlinlang.org/u/broot)\
**Post date:** [July 12, 2021, 2:27pm UTC](https://discuss.kotlinlang.org/t/creating-a-listener-without-an-interface/22388/6 "2021-07-12T14:27:44Z")

</div>

Well, if you compile Kotlin to JVM bytecode, it really works in the very same way as in Java - it uses interfaces. I guess it can’t be different, because JVM does not understand anything else.

`(marsProperty:MarsProperty) -> Unit` is internally represented as `Function1<MarsProperty, Unit>`. `Function1` is an interface with `invoke()` method. `clickListener(marsProperty)` line basically calls: `clickListener.invoke(marsPropery)`.

Note that lambdas in Java work in exactly the same way. You provide lambda to some function, but internally it is converted to an implementation of some interface. This is sometimes called “SAM conversion”.

---

<div class="post-metadata">

**Author:** ![artan.berisha](https://avatars.discourse-cdn.com/v4/letter/a/eada6e/32.png) [@artan.berisha](https://discuss.kotlinlang.org/u/artan.berisha)\
**Post date:** [July 12, 2021, 2:30pm UTC](https://discuss.kotlinlang.org/t/creating-a-listener-without-an-interface/22388/7 "2021-07-12T14:30:49Z")

</div>

Hi @cylab , I saw the link and it shows a way to use a lambda to implement with less code an interface, but in the code I showed above there isn’t any `Interface` (there is just the class named `OnClickListener` like @broot showed above) declared like `IntPredicate` interface on the link you provided.

---

<div class="post-metadata">

**Author:** ![broot](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@broot](https://discuss.kotlinlang.org/u/broot)\
**Post date:** [July 12, 2021, 2:42pm UTC](https://discuss.kotlinlang.org/t/creating-a-listener-without-an-interface/22388/8 "2021-07-12T14:42:29Z")

</div>

Actually, there is an interface in my above code. `Consumer` is an interface and it is the direct equivalent of the Kotlin’s lambda.

Also, if you are confused with your above code, then this is understandable, because this code is in fact confusing. Maybe someone created it to provide Java-\>Kotlin interoperability, but other than that it doesn’t make too much sense. I don’t see the point of creating this `OnClickListener` - it is just a wrapper around a function object, but it does not do anything more.

---

<div class="post-metadata">

**Author:** ![cylab](https://avatars.discourse-cdn.com/v4/letter/c/b5ac83/32.png) [@cylab](https://discuss.kotlinlang.org/u/cylab)\
**Post date:** [July 12, 2021, 2:45pm UTC](https://discuss.kotlinlang.org/t/creating-a-listener-without-an-interface/22388/9 "2021-07-12T14:45:10Z")

</div>

Ok. It seems, I didn’t correctly get the context. Can you show a usage example of this OnClickListener class?

---

<div class="post-metadata">

**Author:** ![artan.berisha](https://avatars.discourse-cdn.com/v4/letter/a/eada6e/32.png) [@artan.berisha](https://discuss.kotlinlang.org/u/artan.berisha)\
**Post date:** [July 12, 2021, 2:50pm UTC](https://discuss.kotlinlang.org/t/creating-a-listener-without-an-interface/22388/10 "2021-07-12T14:50:21Z")

</div>

You are right, I probably should have added this from the start, [here](https://github.com/udacity/andfun-kotlin-mars-real-estate/blob/master/app/src/main/java/com/example/android/marsrealestate/overview/PhotoGridAdapter.kt) is the example I was following.  
And [here](https://classroom.udacity.com/courses/ud9012/lessons/ee5a525f-0ba3-4d25-ba29-1fa1d6c567b8/concepts/a24d8fdb-64ab-419c-bb29-c1225e104fa9) is the exact part of the course when this part is implemented. (\*Note: this was used on some other part of the course but this is one of the cases I was looking)

---

<div class="post-metadata">

**Author:** ![broot](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@broot](https://discuss.kotlinlang.org/u/broot)\
**Post date:** [July 12, 2021, 2:57pm UTC](https://discuss.kotlinlang.org/t/creating-a-listener-without-an-interface/22388/11 "2021-07-12T14:57:43Z")

</div>

So what is the most confusing to you? Te fact that `onClickListener` isn’t an interface, but a class?

---

<div class="post-metadata">

**Author:** ![cylab](https://avatars.discourse-cdn.com/v4/letter/c/b5ac83/32.png) [@cylab](https://discuss.kotlinlang.org/u/cylab)\
**Post date:** [July 12, 2021, 2:59pm UTC](https://discuss.kotlinlang.org/t/creating-a-listener-without-an-interface/22388/12 "2021-07-12T14:59:36Z")

</div>

Yeah, this is just a pointless wrapping of the lambda. In line 77 of

```auto
          override fun onBindViewHolder(holder: MarsPropertyViewHolder, position: Int) {
              val marsProperty = getItem(position)
              holder.itemView.setOnClickListener {
/* 77 */ onClickListener.onClick(marsProperty)
              }
              holder.bind(marsProperty)
          }

```

the `onClick()` function of that `OnClickListener` is just called inside a SAM-conversion lambda that is passed to `setOnClickListener`.

Edit: To elaborate further, `holder.itemView.setOnClickListener` expects an interface, which is on-the-fly implemented in SAM-conversion manner by the curly braces. The single method of the interface contains the call to `onClick()` of that custom `OnClickListener`, that in turn forwards to the `clickListener` function passed in the constructor of `OnClickListener`.

It’s just wrapping of the Kotlin function inside an object, which frankly is just convoluted, misleading and confusing. They could just have opted to accept the original Interface in the `PhotoGridAdapter`s constructor, or directly accept the `clickListener` function there instead.

---

<div class="post-metadata">

**Author:** ![artan.berisha](https://avatars.discourse-cdn.com/v4/letter/a/eada6e/32.png) [@artan.berisha](https://discuss.kotlinlang.org/u/artan.berisha)\
**Post date:** [July 13, 2021, 6:31am UTC](https://discuss.kotlinlang.org/t/creating-a-listener-without-an-interface/22388/13 "2021-07-13T06:31:27Z")

</div>

Yes @broot, that is confusing 😕 , but now I saw [this](https://stackoverflow.com/questions/66541938/kotlin-custom-onclicklistener-interface) answer on stackoverflow and it says the following:

> " This is most likely a hack from before Kotlin 1.4 to allow a pseudo-interface to be constructed with a lambda. Before Kotlin 1.4, you could only use [SAM conversion] with Java interfaces."

And also it says that:

> And you can’t make some multi-purpose class implement the interface, because there is no interface.

And now reading the @cylab answer I think its becoming clear.  
Thank you guys 🙂

---

<div class="post-metadata">

**Author:** ![yamin](https://avatars.discourse-cdn.com/v4/letter/y/c6cbf5/32.png) [@yamin](https://discuss.kotlinlang.org/u/yamin)\
**Post date:** [July 13, 2021, 10:28am UTC](https://discuss.kotlinlang.org/t/creating-a-listener-without-an-interface/22388/14 "2021-07-13T10:28:22Z")

</div>

> [@cylab](#):
>
> Yeah, this is just a pointless wrapping of the lambda. In line 77 of
> 
> ```auto
> override fun onBindViewHolder(holder: MarsPropertyViewHolder, position: Int) {
> val marsProperty = getItem(position)
> holder.itemView.setOnClickListener {
> /* 77 */ onClickListener.onClick(marsProperty)
> }
> holder.bind(marsProperty)
> }
> 
> ```
> 
> the `onClick()` function of that `OnClickListener` is just called inside a SAM-conversion lambda that is passed to `setOnClickListener` .
> 
> Edit: To elaborate further, `holder.itemView.setOnClickListener` expects an interface, which is on-the-fly implemented in SAM-conversion manner by the curly braces. The single method of the interface contains the call to `onClick()` of that custom `OnClickListener` , that in turn forwards to the `clickListener` function passed in the constructor of `OnClickListener` .
> 
> It’s just wrapping of the Kotlin function inside an object, which frankly is just convoluted, misleading and confusing. They could just have opted to accept the original Interface in the `PhotoGridAdapter` s constructor, or directly accept the `clickListener` function there instead.

They should criminalize this way of programming (pointless wrapping of the lambda)!  
and also implementing `clicklistener` inside of `onBindViewHolder`? I’m insulted!

---

<div class="post-metadata">

**Author:** ![dmitry](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/dmitry/32/10200_2.png) [@dmitry](https://discuss.kotlinlang.org/u/dmitry)\
**Post date:** [July 22, 2021, 11:38am UTC](https://discuss.kotlinlang.org/t/creating-a-listener-without-an-interface/22388/15 "2021-07-22T11:38:57Z")

</div>

> You provide lambda to some function, but internally it is converted to an implementation of some interface.

Not exactly, modern Java doesn’t implement an interface but takes advantage of `invokeDynamic` instruction to optimize the generated code. Check [here](https://www.baeldung.com/java-invoke-dynamic) for details.

---

<div class="post-metadata">

**Author:** ![broot](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@broot](https://discuss.kotlinlang.org/u/broot)\
**Post date:** [July 22, 2021, 4:27pm UTC](https://discuss.kotlinlang.org/t/creating-a-listener-without-an-interface/22388/16 "2021-07-22T16:27:16Z")

</div>

Thanks for pointing this out. I must admit I don’t fully understand how `invokedynamic` works. Is it technically accurate to say that lambdas are still converted to interface implementations, but the process is two-step and it happens partially at compile time and partially at runtime?
