I have a Kotlin helper class defined as:
class CountdownTimer(endDateInSeconds: Long, callback: (timeRemaining: RemainingTime) -> Unit)
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.
I can use this class from Kotlin cleanly:
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, even though the anonymous function specifies a Unit return type (which in theory is the equivalent of a Java void return type):
this.countdownTimer = new CountdownTimer(this.endDate, remainingTime -> {
var timeString = // format time remaining into a string
if (view != null) {
view.updateCountdownTimer(timeString);
}
return null;
});
虽然从技术上讲这不是一个问题,但不得不从Java回调中提供一个毫无意义的返回值似乎..错误的在Kotlin中有没有更好的方式来表达这个回调?