我试图为ThreeTen's drop-in Scala reimplementation of java.time
中的七个日期时间类创建参数化类Interval
。我班的签名看起来是这样的:Tricky类型边界
final case class Interval[T <: Temporal with Ordered[T] with Serializable](start: T, end: T)
能正常工作时T
为Instant
,LocalTime
,OffsetDateTime
,或OffsetTime
,但未能编译LocalDate
,LocalDateTime
和ZonedDateTime
因为后三个不直接继承从Ordered[T]
。我无法提供适用于所有七个人的Interval[T]
的正确类型签名。
有更多经验的人能帮我一个忙吗?特别感谢您解释答案背后的理论!
仅供参考,这里有七个日期时间类的签名:
final class Instant private(private val seconds: Long, private val nanos: Int) extends TemporalAccessor with Temporal with TemporalAdjuster with Ordered[Instant] with Serializable
final class LocalTime(_hour: Int, _minute: Int, _second: Int, private val nano: Int) extends TemporalAccessor with Temporal with TemporalAdjuster with Ordered[LocalTime] with Serializable
final class OffsetDateTime private(private val dateTime: LocalDateTime, private val offset: ZoneOffset) extends Temporal with TemporalAdjuster with Ordered[OffsetDateTime] with Serializable
final class OffsetTime(private val time: LocalTime, private val offset: ZoneOffset) extends TemporalAccessor with Temporal with TemporalAdjuster with Ordered[OffsetTime] with Serializable
final class LocalDate private(private val year: Int, monthOfYear: Int, dayOfMonth: Int) extends ChronoLocalDate with Temporal with TemporalAdjuster with Serializable
final class LocalDateTime private(private val date: LocalDate, private val time: LocalTime) extends ChronoLocalDateTime[LocalDate] with Temporal with TemporalAdjuster with Serializable
final class ZonedDateTime(private val dateTime: LocalDateTime, private val offset: ZoneOffset, private val zone: ZoneId) extends ChronoZonedDateTime[LocalDate] with Temporal with Serializable
下面是对于那些过去三年Chrono*
性状类特征:
trait ChronoLocalDate extends Temporal with TemporalAdjuster with Ordered[ChronoLocalDate]
trait ChronoLocalDateTime[D <: ChronoLocalDate] extends Temporal with TemporalAdjuster with Ordered[ChronoLocalDateTime[_]]
trait ChronoZonedDateTime[D <: ChronoLocalDate] extends Temporal with Ordered[ChronoZonedDateTime[_]]