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

287
Vistas
Name conflicts when using Method extension and Inheritance

I have an extension

fun Fragment.showToast(message: String, toastLength: Int = Toast.LENGTH_SHORT) {
    Toast.makeText(context, message, toastLength).show()
}

In project we're using MVP:

interface MyContract {
    interface View {
        fun showToast(message: String)
    }
}

class MyFragment : Fragment(), MyContract.View {
    override fun showToast(message: String) {
        showToast(message)
    }
}

So in MyFragment.showToast(message) I expect to call extension function instead of StackOverflowException.

Is it possible to call extension function directly? Something like:

Fragment.showToast(this, message)

or I just need to give another name?

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

You could cast your MyFragment class to Fragment and do:

class MyFragment : Fragment(), MyContract.View {
    override fun showToast(message: String) {
        val fragment = this as Fragment
        fragment.showToast(message)
    }
}

This way you will refer to the extension function of the Fragment class

over 4 years ago · Santiago Trujillo Denunciar

0

Instance methods have precedence over extension functions, in case of signature conflict, so the behaviour you're experiencing is the expected one.

You can refer directly to shadowed extension function importing it with an alias: pretend you have a file FragmentExt.kt containing the extension method in a package named mypkg:

package mypkg

fun Fragment.showToast(message: String, toastLength: Int = Toast.LENGTH_SHORT) {
    Toast.makeText(context, message, toastLength).show()
}

So, you can invoke extension function (avoiding to call instance method) as follows:

import mypkg.showToast as extShowToast
val fragment : Fragment = ...
fragment.extShowToast(message)

This approach works both in the MyFragment subclass and in any client class.

In Java code, you can do something similar by accessing Kotlin extension method as a static method on a class names by the file name: if your extension function is in FragmentExt.kt, Java code can point to extension method using FragmentExtKt.showToats("Message here").

over 4 years ago · Santiago Trujillo Denunciar

0

You've got two methods that you can call on a Fragment instance with the exact same name and the parameters.

There's no way to distinguish which one you want to call.

I'd change the name of one.

E.g.

fun Fragment.toast(message: String, toastLength: Int = Toast.LENGTH_SHORT) {
    Toast.makeText(context, message, toastLength).show()
}
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