jw
07/25/2019, 9:23 PMilya.gorbunov
07/25/2019, 9:58 PMjw
07/25/2019, 10:07 PMelapsed()
jw
07/25/2019, 10:31 PMvar expiration = expirationClockMark
val mark = clock.mark()
if (mark.elapsed() >= expiration.elapsed()) {
where it feels like it should be
var expiration = expirationClockMark
val mark = clock.mark()
if (mark >= expiration) {
There's no type system tricks (as far as I know) to restrict this at compile time to two instances which originated from the same Clock
.
But maybe a runtime check is enough? Things like java.nio.path.Path
come to mind where it's tied to a FileSystem
and you cannot meaningfully do operations on two `Path`s from different `FileSystem`s. Despite this, there are methods which work on two instances (like Path.resolve(Path)
) which just runtime-check the invariant.
The only problem I see is that it would become the responsibility of any custom `Clock`/`ClockMark` implementation to ensure they implement this behavior.ilya.gorbunov
07/25/2019, 10:57 PMIf these two marks are from the same clock, this expression would be almost always true or always false, becauseif (mark.elapsed() >= expiration.elapsed())
elapsed
values of both of them increment simultaneously.
What are you trying to achieve by this comparison?jw
07/25/2019, 10:59 PMSystem.nanoTime()
)ilya.gorbunov
07/25/2019, 11:00 PMilya.gorbunov
07/25/2019, 11:03 PMval expiredClockMark = clock.mark() + timeout
...
if (!expiredClockMark().elapsed.isNegative()) {
// expired
}
jw
07/25/2019, 11:03 PMilya.gorbunov
08/08/2019, 4:55 PMexpirationClockMark
value and how did your code look in the end after rewriting?jw
08/08/2019, 5:01 PMclock.mark() > expirationClockMark
) or do math on them (take this mark plus a duration to produce a new mark, expirationClockMark = currentMark + expirationDuration
). This probably stems from java.time.Clock use which produces Instants which are points on the timeline where this code would have worked.
After re-working, based on your help, the final code is at https://github.com/JakeWharton/SdkSearch/blob/master/backend/dac-proxy/src/main/java/com/jakewharton/sdksearch/proxy/memoize.ktjw
08/08/2019, 5:06 PMclock: () -> Int = System::nanoTime
and expirationNanos: Int
to Clock
and ClockMark
, so that also led me to naively assume they represented the same concepts