Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Kotlin @不限制DSL范围的DSL标记_Kotlin_Dsl - Fatal编程技术网

Kotlin @不限制DSL范围的DSL标记

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注释没有限制此场景中的范围,以及我如何修复它吗?它似乎在注释基类,在本例中是标记,这是否意味着所有其他注

我正在为HTML构建一个Kotlin DSL,以满足我的特定需求(因此不使用kotlinx.HTML)

在上面的示例中,我可以在
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>()