4Manuals

  • PDF Cloud HOME

@DSLMarker不限制DSL的范围 Download

    类型“文件”的表达式“目标”不能作为函数调用 如何在导航图中将包含的图设置为startDestination? 如何使用带有自定义入口点的FlutterFragment在我的Android应用中嵌入Flutter布局? 在协程中使用runBlocking会发生什么不好的事情吗? 是否热衷于为包含所有依赖项的kotlin项目构建fatjar? Kotlin中的arrayListOf和ArrayList有什么区别? ViewPager2-滑块过渡滑动得太快 我应该在基于控制器的Spring Webflux服务中将Flow用作返回类型吗? 您可以在屏幕上更改通知位置吗? 为什么Kotlin使用Internal而不是package?

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

        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

类似地,A类也被标记为: https://github.com/persephone-unframework/dsl/blob/master/src/main/kotlin/io/persephone/dsl/element/A.kt

@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 个答案:

答案 0 :(得分:0)

似乎在对基类(在这种情况下为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>()

enter image description here



Similar searches
    使用Rabbit的pika确认消息 如何在地图中放置对象的出现? 使用把手更改字体大小 如何在浏览器中实现类似Google Spreadsheet的应用程序? Rails-file.readlines与file.gets