Kotlin higher order function call from Java

I have a Kotlin helper class defined as:

class CountdownTimer(endDateInSeconds: Long, callback: (timeRemaining: RemainingTime) -> Unit)

which, as the name implies, takes an era time and a callback to call at fixed intervals (in this case seconds) until the end date is reached. RemainingTime is a data class containing the amount of time (seconds, minutes, hours, etc.) until the end date.

I can use this class from Kotlin purely:

        countdownTimer = CountdownTimer(endDate, { timeRemaining ->
             var timeString = // format time remaining into a string
             view?.updateCountdownTimer(timeString)
         })

However, when I call this from Java, I am forced to provide an unnecessary return value in the callback, despite the fact that the anonymous function indicates the type of the returned module (which is theoretically equivalent to the return type of Java void):

        this.countdownTimer = new CountdownTimer(this.endDate, remainingTime -> {
             var timeString = // format time remaining into a string
             if (view != null) {
                 view.updateCountdownTimer(timeString);
             }
             return null;
        });

, Java .. . ?

+4
1

Unit object void. kotlin return Unit.INSTANCE; . , , void.

+5

Source: https://habr.com/ru/post/1685184/


All Articles