Kotlin @不限制DSL范围的DSL标记
我正在为HTML构建一个Kotlin DSL,以满足我的特定需求(因此不使用kotlinx.HTML) 在上面的示例中,我可以在Kotlin @不限制DSL范围的DSL标记,kotlin,dsl,Kotlin,Dsl,我正在为HTML构建一个Kotlin DSL,以满足我的特定需求(因此不使用kotlinx.HTML) 在上面的示例中,我可以在a的任何子元素中调用href,而无需执行this@a.href=''。在本例中,如何限制范围,使this仅属于DIV类型,并在调用href时抛出编译器错误,因为DIV没有href属性 下面是DIV类的简化版本 同样,A类也标记为: 你知道为什么@DslMarker注释没有限制此场景中的范围,以及我如何修复它吗?它似乎在注释基类,在本例中是标记,这是否意味着所有其他注
a
的任何子元素中调用href
,而无需执行this@a.href=''
。在本例中,如何限制范围,使this
仅属于DIV
类型,并在调用href
时抛出编译器错误,因为DIV
没有href
属性
下面是DIV
类的简化版本
同样,A类也标记为:
你知道为什么
@DslMarker
注释没有限制此场景中的范围,以及我如何修复它吗?它似乎在注释基类,在本例中是标记
,这是否意味着所有其他注释都没有用
@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>()
@DslMarker
注释类标记标记器
@标记器
抽象类标记(val标记名:String,var selfClosing:Boolean=false):元素{
val children=arrayListOf()
val attributes=hashMapOf()
@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
}
....
}
@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>()