I’m trying to pass `emptyList()` to `.defaultValue...
# squarelibraries
j
I’m trying to pass
emptyList()
to
.defaultValue("%L", defaultValue)
but it generate an array instead :
public val myList: List<String> = []
which fails. How can I fix it to generate
public val myList: List<String> = emptyList()
instead?
c
how did you define
defaultValue
that you pass to the method?
j
I used this code
Copy code
val defaultValue: List<String> = emptyList<String>()
ParameterSpec.builder(
    name = "myList",
    type = List::class.java.asClassName()
        .parameterizedBy(String::class.java.asClassName())
)
    .defaultValue("%L", defaultValue)
    .build()
c
you need to define a
MemberName
to get a reference to a function/member. what your code is doing is evaluate
emptyList
and put that value as a literal. See https://square.github.io/kotlinpoet/#m-for-members What you need is
Copy code
val emptylist = MemberName("kotlin.collections", "emptylist")

ParameterSpec.builder("myList", List::class.parameterizedBy(String::class))
            .defaultValue("%M()", emptylist)
            .build()
j
Thanks!