0

I am trying to generate one kotlin class using kotlin poet library which should have one function and one inner static class as given below.

class SampleClass{

        class TestClass{
            lateinit var id: String
            lateinit var name: String
        }

        fun function1(init: TestClass.() -> Unit) {
            val trackPhoneNumberClicked = TestClass().apply(init)

            val event = Event.Builder.from(testData.getTestDataById("testdataid")!!)
                    .apply {
                        addProperty("id", trackPhoneNumberClicked.id)
                        addProperty("name", trackPhoneNumberClicked.name)
                    }
                    .build()
        }
    }

I can generate SampleClass and inner class TestClass but I am not able to create function1 with this argument and body.

github like of kotlin poet library. https://github.com/square/kotlinpoet

Can anyone provide any solution for this?

Rock
  • 9
  • 1
  • 8
  • 1
    I recommend that you edit your question and provide a [mcve] of the code that you are currently using ("I can generate SampleClass and inner class TestClass"), and explain a bit more what your specific problems are ("I am not able to create function1 with this argument and body"). The sample code on the KotlinPoet project page shows how to create a function with an argument and body, so we need to know more about what is not working for you (compile errors? wrong output? something else?). – CommonsWare Dec 15 '18 at 20:55

1 Answers1

0

I got the answer that how we can generate function1(init: TestClass.() -> Unit) this type of argument in function. We need to use LambdaTypeName class of kotlin poet lib.

val buildParameter = ParameterSpec.builder(FUNCTION_PARAMETER_NAME, LambdaTypeName.get(ClassName("", CLASS_TYPE_NAME), returnType = Unit::class.asTypeName())).build()

Rock
  • 9
  • 1
  • 8