我正在为HTML构建Kotlin DSL,以满足我的特定要求(因此不使用kotlinx.html) 在上面的示例中,我可以在 这是 类似地,A类也被标记为:
https://github.com/persephone-unframework/dsl/blob/master/src/main/kotlin/io/persephone/dsl/element/A.kt 有人知道为什么 答案 0 :(得分:0) 似乎在对基类(在这种情况下为 DIV(classes = "div1") {
+"text1"
a(href = "#0") {
+"text2"
div(classes = "div2") {
+"text3"
href = "#1"
}
div(classes = "div3") {
+"text4"
href = "#2"
}
}
hr(classes = "hr1")
span(classes = "span1") {
+"text5"
}
}
href
的任何子元素中调用a
,而不必执行this@a.href = ""
。我如何限制范围,以使this
在此示例中仅为DIV
类型,并且在调用href
时引发编译器错误,因为DIV
没有{ {1}}属性?href
类的简化版本
https://github.com/persephone-unframework/dsl/blob/master/src/main/kotlin/io/persephone/dsl/element/DIV.kt DIV
@DslMarker
annotation class DivMarker
@DivMarker
class DIV(
classes: String? = null,
....
init: (DIV.() -> Unit)? = null
) : Tag(
tagName = "div",
selfClosing = false
) {
fun a(
classes: String? = null,
....
init: (A.() -> Unit)? = null
) = A().let {
this.children.add(it)
....
init?.invoke(it)
it
}
....
}
@DslMarker
annotation class AMarker
@AMarker
class A(
href: String? = null,
...
init: (A.() -> Unit)? = null
) : Tag(
tagName = "a",
selfClosing = false
) {
fun div(
classes: String? = null,
init: (DIV.() -> Unit)? = null
) = DIV().let {
this.children.add(it)
....
init?.invoke(it)
it
}
....
}
注释不限制这种情况下的范围以及如何解决它吗?1 个答案:
Tag
进行注释)中是否有用,这可能意味着所有其他注释都没有作用?@DslMarker
annotation class TagMarker
@TagMarker
abstract class Tag(val tagName: String, var selfClosing: Boolean = false): Element {
val children = arrayListOf<Element>()
val attributes = hashMapOf<String, String>()