Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

142
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!