title: Interface as functions in Kotlin
body: <p>I am working on an android library that contains some views. Naturally these views can emit events. </p>
  
  <p>I have an interface called (just for the purpose of this question) <code>Listener</code>. If I wrote the library in Java things would look like this:</p>
  
  <pre class="lang-java prettyprint-override"><code>public interface Listener {
      void onEvent();
  }
  </code></pre>
  
  <pre class="lang-java prettyprint-override"><code>public class SomeView extends FrameLayout {
      // Some more functions and implementation details
  
      public void setListener(Listener l) { ... }
  }
  </code></pre>
  
  <p>When using this view in a Kotlin activity I can use the <code>setListener</code> like this:</p>
  
  <pre><code>someViewInstance.setListener {
      // implementation
  }
  </code></pre>
  
  <p>I want to write my library in Kotlin, but it might be used in Java code as well, so I want to provide and interface for the listener just like a regular view (like above) but have the option for Kotlin code to use the function implementation:</p>
  
  <pre><code>interface Listener {
      fun onEvent()
  }
  </code></pre>
  
  <p>when I try to use <code>setListener</code> like above in my Kotlin test activity I get a compilation error saying that the function expects type <code>Listener</code> but got <code>() -&gt; Unit</code>.</p>
  
  <p>Is there a way to enable this kind of implementation in Kotlin without having to create a new function for this?</p>
  
  <p>I thought about having just one function that receives <code>() -&gt; Unit</code> but then it look weird in the Java code (<code>Function1</code> etc.).</p>
  
  <p>Thanks!</p>