Stumped by Operator Overloading koan

Hi! First post here - I am new to Kotlin, though I’ve programmed in a few other languages …

Anyway, I am working through the Kotlin Koans series. So far most of them have been pretty easy, but I am struggling with the operator overloading problem.

[I would like *hints*, please, not a complete solution!]

It seems to me that the key issue is that there is no sensible way for multiplication of an enum constant to return an atomic value, so a TimeInterval.times operator function must return some kind of object - e.g. CompoundInterval(baseInterval: YEAR, qty: 5). In which case the MyDate.plus function must be able to accept either the constant or the object.

The following code is my latest attempted solution. It doesn’t work; I’m pretty sure this approach could work, but it also seems overly complex - and it requires a slight modification to MyDate.addTimeIntervals, which is probably not supposed to be necessary.

So, am I on the right track at all? If not, what am I missing? (again, hints only, please!)

import TimeInterval.*

interface ITimeInterval

data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int)

// Supported intervals that might be added to dates:
enum class TimeInterval { DAY, WEEK, YEAR } : ITimeInterval

data class CompoundInterval(val baseInterval: ITimeInterval, val qty: Int) : ITimeInterval

operator fun TimeInterval.times(n: Int) : CompoundInterval {
    return CompoundInterval(enumValueOf(this.name), n)
}

operator fun MyDate.plus(timeInterval: ITimeInterval): MyDate {
    if (timeInterval is TimeInterval) {
        return MyDate.addTimeIntervals(timeInterval, 1)
    } else {
        return MyDate.addTimeIntervals(timeInterval.baseInterval, timeInterval.qty)
    }
}

fun task1(today: MyDate): MyDate {
    return today + YEAR + WEEK
}

fun task2(today: MyDate): MyDate {
    TODO("Uncomment") //return today + YEAR * 2 + WEEK * 3 + DAY * 5
}

By the way, what is the purpose of the import TimeInterval.* statement on Line 1?

I checked the solution you are on the right track but to be honest the solution is very weird and I don’t understand it. If you figure it out and understand it please respond here with an explanation :wink: