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

143
Vistas
Class method signature based on supplied type in Kotlin?

I'm trying to reduce boilerplate on something I'm working on and wondering if something is possible - I suspect it's not but was looking for confirmation

class Something<T> {

private val list = mutableListOf<T>()

fun addToList(value: T) = list.add(value) }

So if I wanted to use this with a class like:

class Data(number: Int, letter: Char)

I'd have to use addToList like:

addToList(Data(1,"a"))

Is there some way to use the supplied type T to construct the method addToList dynamically? So that the class would be instantiated like:

val thing = Something<Data>()

but then addToList were called like

addToList(1,"a")

Like I said, don't think this is possible but was looking for confirmation.

What I was really trying to do was come up with something that would allow me to do this without declaring Data at all, but instead just define the structure and the subsequent addToList method when Something() was instantiated - not sure if I have described this all that well but if anyone has any suggestions in general around that I'd be grateful!

Thanks!

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

0

There are Pair and Triple tuple classes provided in the standard library which allows you to avoid declaring a class for simple combinations of values. If you need more than 3 parameters of different types, you'd need to create your own class or use a library that provides larger tuple classes. If all types are the same, you can use List instead of a tuple.

In my opinion even Triple is pushing it and anything with more than two distinct properties should just have its own data class defined.

class Something<A, B> {

    private val list = mutableListOf<Pair<A, B>>()

    fun addToList(valueA: A, valueB: B) = list.add(Pair(valueA, valueB)) 

}

val something = Something<Int, String>()
something.addToList(1, "a")

An alternate approach if you want to keep the flexibility of your Something class to hold anything would be to use an extension function.

class Something<T> {

    private val list = mutableListOf<T>()

    fun addToList(value: T) = list.add(value)

}

fun <A, B> Something<Pair<A, B>>.addToList(valueA: A, valueB: B) = 
    addToList(Pair(valueA, valueB))

val something = Something<Pair<Int, String>>()
something.addToList(1, "a")
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