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

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

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 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