Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

216
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda