How I can set constructors on TypeSpec.anonymousCl...
# squarelibraries
i
How I can set constructors on TypeSpec.anonymousClassBuilder()?
m
Aren't anonymousClasses
object
(for which there are no constructors because they are singletons)?
i
well Javapoet allows it, o.O
m
What is the Kotlin code you'd like to generate ?
e
even if Javapoet allows it, Java doesn't: https://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html "you cannot declare constructors in an anonymous class"
if you're trying to pass arguments to the anonymous class's super constructor, use
addSuperclassConstructorParameter
i
Didn't work
e
can you give an example of what didn't work?
i
Copy code
childElementBinders.put("channel", object : NestedChildElementBinder<ValueHolder> {
      init {
        childElementBinders = HashMap<String, ChildElementBinder<ValueHolder>>()
e
oh that's not a constructor, that's an initializer block
i
oh
e
as a simple demo, the snippet produces this:
Copy code
package example

import java.util.HashMap
import kotlin.String
import kotlin.Unit

public fun main(): Unit {
  childElementBinders.put("channel", object : NestedChildElementBinder<ValueHolder>() {
    init {
      childElementBinders = HashMap<String, ChildElementBinder<ValueHolder>>()
    }
  })
}