奇妙なバグに遭遇しましたが、なぜそれが発生するのかわかりません。元の関数を呼び出すroundToMidnight()
と、呼び出されず、日付が丸められません。
私の元の機能、うまくいかないもの:
suspend operator fun invoke(reference: Reference) = reference.tagId
?.let { tagRepository.getTag(it) }
?.uploadDate ?: Date()
.apply { time += accountRepository.getAccount().first().defaultExpiryPeriod }
.roundToMidnight()
}
何が機能しますか:
suspend operator fun invoke(reference: Reference): Date {
val date = reference.tagId
?.let { tagRepository.getTag(it) }
?.uploadDate ?: Date()
.apply { time += accountRepository.getAccount().first().defaultExpiryPeriod }
return date.roundToMidnight()
}
roundToMidnight()
の新しいインスタンスを返しますDate
fun Date.roundToMidnight(): Date {
val calendar = Calendar.getInstance()
calendar.time = this
calendar[Calendar.HOUR_OF_DAY] = 23
calendar[Calendar.MINUTE] = 59
calendar[Calendar.SECOND] = 59
calendar[Calendar.MILLISECOND] = 0
return Date(calendar.timeInMillis)
}
両方の機能の違いの原因は何ですか? それらはまったく同じであると思います。これが起こるのを忘れていたので、1か月以内にバグのない関数を元の関数にリファクタリングしているのを見ています。