How to overload an operator

I’m trying to work on the koans, but I’m completely stumped by the “Operators overloading” task. The documentation page only tells us the conventions on operators, and says nothing about how to overload an operator yourself. For example, in this task I need to add the plus operator to the MyDate class, evidently by extension. I tried operator fun TimeInterval): MyDate = MyDate.addTimeIntervals(timeInterval, 1) but the compiler complains that it can’t resolve a reference to addTimeIntervals. A full example in the documentation would be great.

You are trying to call a static method “MyDate.addTimeIntervals()”. I think what you want is “this.addTimeIntervals()”

operator fun TimeInterval): MyDate = this.addTimeIntervals(timeInterval, 1)

Hmm, I could have sworn I’d tried using this instead and gotten a similar error, but now the test runs fine (as far as that method is concerned). Still, an example on the documentation page is really needed IMO. Not sure who’s responsible for that.

If you feel that an example could improve the documentation page, you can propose it as a pull request. Notice that “Edit page” button in the upper right corner.