//The below function is the entrance point, and then the class below it is where the problem is fun TagConsumer.template(classes : String? = null, block : TEMPLATE.() -> Unit = {}) : String = TEMPLATE(attributesMapOf("class", classes), this).finalizeDocFrag(this, block) @Suppress("unused") open class TEMPLATE(initialAttributes: Map, override val consumer: TagConsumer<*>) : HTMLTag("template", consumer, initialAttributes, null, false, false), FlowMetaDataPhrasingContent { inline fun generateDocFragContent(consumer: TagConsumer, block: Any) { console.log("got here") println("got here") println("string is r: "+(String is R)) if(String is R) (this as Tag).visit(block as (Tag.() -> Unit)) else finalize(consumer as TagConsumer, block as (TagConsumer.() -> HTMLElement)) } inline fun finalizeDocFrag(consumer: TagConsumer, block: Any?) : R { if (this.consumer !== consumer) { throw IllegalArgumentException("Wrong exception") } val tmp = consumer is HTMLStreamBuilder println("$tmp") println("consumer's class: ${consumer::class.simpleName}") println("R's class: ${R::class.simpleName}") if(block !== null) { if (String is R) return (this as Tag).visitAndFinalize(consumer, block as (Tag.() -> Unit)) else return finalize(consumer as TagConsumer, block as (TagConsumer.() -> HTMLElement)) as R } return consumer.finalize() } fun finalize(consumer: TagConsumer, block: (TagConsumer.() -> HTMLElement)) : HTMLElement { this.visit{} val template = consumer.finalize() as HTMLTemplateElement val content = document.create.block() template.content.appendChild(content); return template } }