https://kotlinlang.org logo
#codereview
Title
# codereview
c

christophsturm

04/28/2021, 3:50 PM
I’m trying to use dsl marker to allow some methods only on the root , some only on the leaf, and some on both. but it seems to not work. I cannot call `rootAndLeaf`despite it being in an interface without dsl marker. maybe the dsl marker is inherited?
Copy code
@DslMarker
annotation class MyDSL

@MyDSL
interface RootOnly {
    fun rootOnly()
}
interface RootAndLeaf : RootOnly {
    fun call(function: Leaf.() -> Unit)
    fun rootAndLeaf()
}
@MyDSL
interface Leaf {
    fun leaf()
}

val block: RootAndLeaf.() -> Unit = {
    call {
        leaf()
        rootAndLeaf() // cannot be called by implicit receiver
    }
}
r

Roukanken

04/28/2021, 7:39 PM
messing with it a bit, it's definitely inherited when you flip the implicit recievers (eg, Leaf -> RootAndLeaf -> ..., instead of RootAndLeaf -> Leaf -> ..., that you have), then
leaf()
starts being blocked and
rootAndLeaf()
works if you just want to fix it tho, it's easy to just flip the inheritance. eg,
Root
,
Leaf
inheriting from
RootAndLeaf
fixes stuff, and makes more sense to model it like that
2 Views