I try to write a generic method that forwards an Instant
or an OffsetDateTime
to a given LocalTime
. For example, if I have the OffsetDateTime
2023-10-03T18:36:18.29+02:00
and LocalTime
18:00
, then the result should be 2023-10-04T18:00:00+02:00
. Or, if the LocalTime
input is 19:00
, then result should be 2023-10-03T19:00:00+02:00
However, Instant
and OffsetDateTime
miss a common interface, so I decided to give structural types a try.
import reflect.Selectable.reflectiveSelectable
import java.time.*
def forwardTo[
T <: {
def truncatedTo(t: TemporalUnit): T
def `with`(t: LocalTime): T
def isBefore(t: T): Boolean
def plus(a: Long, u: TemporalUnit): T
}
](time: T, localTime: LocalTime): T =
val adjustedTime = time.truncatedTo(ChronoUnit.DAYS).`with`(localTime)
if adjustedTime.isBefore(time) then adjustedTime.plus(1, ChronoUnit.DAYS)
else adjustedTime
Unfortunately, this results in a compiler error:
[error] -- Error: Time.scala:38:28
[error] 38 | if adjustedTime.isBefore(time) then adjustedTime.plus(1, ChronoUnit.DAYS)
[error] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^
[error] |Structural access not allowed on method isBefore because it has a parameter type with an unstable erasure
Is there something I am doing wrong? Am I using the wrong tool for the task at hand? Any feedback is appreciated.
Scastie Link: