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

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

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 Relatório

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