Michael de Kaste
09/03/2023, 5:31 PMMichael de Kaste
09/03/2023, 5:31 PMinterface Incrementable<T> {
fun T.increment(): T
}
Now I can define a class like so:
context(Incrementable<T>)
class Interval<T>(
val toIncluding: T
) : Interval<T> {
val to: T = toIncluding.increment()
}
and then, using a custom object I can do something like this:
object IncrementableLocalDate : Incrementable<LocalDate> {
fun LocalDate.increment() = plusDays(1)
infix fun LocalDate.toIncluding(other: T) = Interval(this, other)
}
this is all fine and dandy for extending usage for existing types, since I don't own LocalDates and typing date toIncluding otherDate
will prompt an import specifically for the IncrementableLocalDate class.Michael de Kaste
09/03/2023, 5:34 PMclass MinuteTime(time: LocalDateTime) : Incrementable<MinuteTime> {
val time = time.truncatedTo(ChronoUnit.MINUTES)
override fun MinuteTime.increment() = MinuteTime(time.plusMinutes(1))
}
Sadly, the class existing and spefically creating a context for incrementable, doesn't work for calling the constructor of Interval:
val minuteTime = MinuteTime(LocalDateTime.now())
Interval(minuteTime) // No required context receiver found: Incrementable<T>: Ctx { class Interval }
So my question now becomes; how do I fix this so it works for both owned and unowned types?Youssef Shoaib [MOD]
09/03/2023, 6:23 PMfun <T: Incrementable<T>> Interval(toIncluding: T) = with(toIncluding) { Interval(toIncluding) }
Michael de Kaste
09/03/2023, 8:18 PMwith(x){ x }
should be implicitly possibleYoussef Shoaib [MOD]
09/03/2023, 8:23 PMT: Incrementable<T>
and context(IncrementableT>)
are fundementally different. One easy thing you can at least do is have an object MinuteTimeIncrementable: Incrementable<MinuteTime>
and at the start of your calls do with(MinuteTimeIncrementable)
Davio
09/04/2023, 3:16 PM