Is there a better way to initialize a list of anno...
# ksp
c
Is there a better way to initialize a list of annotated classes when given a
List<KSClassDeclaration>
?
Copy code
val classNames = validDeclarations.map { it.toClassName() }

        TypeSpec
            .objectBuilder("FeatureConfigs")
            .addProperty(
                PropertySpec
                    .builder(
                        name = "values",
                        type = List::class
                            .asClassName()
                            .parameterizedBy(
                                Any::class.asClassName()
                            )
                    )
                    .initializer(
                        buildCodeBlock {
                            addStatement("listOf(")
                            classNames.forEach { token ->
                                add("%T::class", token)
                            }
                            addStatement(")")
                        }
                    )
                    .build()
            )
            .build()
e
more of a kotlinpoet question, isn't it?
c
Yeah, but I didn’t see a kotlinpoet channel, so I thought I’d try my luck.
e
#C5HT9AL7Q
Copy code
.initializer(
    CodeBlock.of("%M(%T)",
        MemberName("kotlin.collections", "listOf"),
        classNames.joinToCode() { token ->
            CodeBlock.of("%T::class", token)
        },
    )
)
c
Oh, awesome. I wasn’t aware of that channel - thank you!
I couldn’t get it to work with that because it expected a type rather than whatever it was receiving. I got it to work by changing the format statement to
"%M(%L)"
I guess it was expecting type and receiving a code block
e
oh that was my mistake, it should have been
%L