https://kotlinlang.org logo
#squarelibraries
Title
# squarelibraries
j

jean

03/11/2023, 10:16 AM
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

Chrimaeon

03/11/2023, 10:54 AM
how did you define
defaultValue
that you pass to the method?
j

jean

03/11/2023, 11:08 AM
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

Chrimaeon

03/11/2023, 11:14 AM
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

jean

03/11/2023, 11:43 AM
Thanks!
5 Views