Home > Software design >  Returning anonymous object from function and infering type in Kotlin
Returning anonymous object from function and infering type in Kotlin

Time:12-03

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.

CodePudding user response:

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

CodePudding user response:

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.

  • Related