title: Calling a Kotlin higher-order function from Java
body: <p>I have a Kotlin helper class defined as:</p>
  
  <pre><code>class CountdownTimer(endDateInSeconds: Long, callback: (timeRemaining: RemainingTime) -&gt; Unit)
  </code></pre>
  
  <p>which as the name implies, takes an epoch time and a callback to be invoked at fixed intervals (seconds in this case) until the end date is reached. RemainingTime is a data class containing the amount of time (seconds, mins, hours etc) until the end date. </p>
  
  <p>I can use this class from Kotlin cleanly:</p>
  
  <pre><code>        countdownTimer = CountdownTimer(endDate, { timeRemaining -&gt;
               var timeString = // format time remaining into a string
               view?.updateCountdownTimer(timeString)
           })
  </code></pre>
  
  <p>However when I call this from Java, I am forced to provide an unnecessary return value in the callback, even though the anonymous function specifies a Unit return type (which in theory is the equivalent of a Java void return type):</p>
  
  <pre><code>        this.countdownTimer = new CountdownTimer(this.endDate, remainingTime -&gt; {
               var timeString = // format time remaining into a string
               if (view != null) {
                   view.updateCountdownTimer(timeString);
               }
               return null;
          });
  </code></pre>
  
  <p>While not technically a problem, having to do provide a meaningless return value from the Java callback seems .. wrong. Is there a better way to express this callback in Kotlin? </p>