Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

219
Views
Kotlin Koans: Operator overloading

I'm completing Kotlin Koans's Comparison exercise and am wondering why compareTo() is the function being overriden but compare() is the function being used.

How do the two functions relate here?

data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int) : Comparable<MyDate> {
    override fun compareTo(otherDate: MyDate): Int = when {
        year != otherDate.year -> year - otherDate.year
        month != otherDate.month -> month - otherDate.month
        else -> dayOfMonth - otherDate.dayOfMonth
    }
}

fun compare(date1: MyDate, date2: MyDate) = date1 < date2
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

The compare() function there just a placeholder shows "how to use compareTo()" without actual meaning. You can change it to other names as you want.

How do the two functions relate here?

A randomly named function compare() calls MyDate's compareTo() function with the comparator symbol <.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!