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

180
Vistas
Returning anonymous object from function and infering type in Kotlin

I'm coming from Java and am new to Kotlin. I am trying to create declarative DSL for unit testing purposes. Here is my attempt to create generic builder method to construct any model object with declarative syntax.

fun <T> having(t: Class<T>) = object {
    infix fun with(fn: T.() -> Unit) = t.newInstance().apply(fn)
}

fun test() {

    having(Pizza::class.java) with {
        size = Size.NORMAL
        cheese = Cheese.MOZARELLA
    }
}

However, the having function returns Any type with unresolved reference to with function. I could extract the anonymous object to it's own class to make it compile, but it would feel redundant. Is it possible to resort on the type inference with functions returning an object instances?

Also, I'm not sure if I'm using generics the idiomatic way here.

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

0

The reason for the having function's return type being inferred as Any is explained in my answer here. Basically, you'll have to make having private in order to make it infer the desired return type. Obviously, that's not a viable solution for you unit testing DSL, which is going to be used by code in other files.

You can consider dropping the word having, and directly declare with as an extension/infix function on KClass instead.

Extension function:

fun <T: Any> KClass<T>.with(fn: T.() -> Unit) = this.createInstance().apply(fn)

// ...

Pizza::class.with {
    size = Size.NORMAL
    cheese = Cheese.MOZZARELLA
}

Infix function:

infix fun <T: Any> KClass<T>.with(fn: T.() -> Unit) = this.createInstance().apply(fn)

fun main() {

    Pizza::class with {
        size = Size.NORMAL
        cheese = Cheese.MOZZARELLA
    }
}
over 4 years ago · Santiago Trujillo Denunciar

0

Instead of returning an object of unspecified type, you can return a wrapping object, which declares the with function and contains the original class object.

This way you get type safety, while retaining you having-style DSL.

data class WrappedInstance<T>(val data: T) {
    infix fun with(applyFn: T.() -> Unit): T = data.also(applyFn)
}

fun <T : Any> having(kClass: KClass<T>): WrappedInstance<T> =
    WrappedInstance(kClass.createInstance())

Using it, looks like this:

data class Pizza(var size: Int = 1, var cheese: Int = 1)

fun test() {
    val pizza: Pizza = having(Pizza::class) with {
        size = 3
        cheese = 5
    }
    
    println(pizza.size)
    println(pizza.cheese)
}

Please be aware, that this as well as your original approach both have a caveat though. They only work for classes with constructors, that don't require any parameter.

See the documentation of KClass<T>.createInstance():

Creates a new instance of the class, calling a constructor which either has no parameters or all parameters of which are optional (see KParameter.isOptional). If there are no or many such constructors, an exception is thrown.


When theres no need for the having part of the DSL, check out @Sweeper's solution.

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