Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

212
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda