https://kotlinlang.org logo
#announcements
Title
# announcements
b

Brandon Ward

07/24/2019, 8:41 PM
Does anyone know if it is possible to exclude specific functions of a type-safe builder from the
@DslMarker
scope verification? See attached example. In case you need a use case to justify my question, my global helper is just something that can supply resources as strings, and it makes sense to be able to do that at various layers of the DSL without needing to repeat the
globalHelper
declaration, but at the same time, other functions not marked as global should have their scope enforced by the compiler so that it helps people use the DSL.
k

karelpeeters

07/24/2019, 8:51 PM
I don't think so, you could specifically repeat the function is every possible scope as a workaround.
😢 1
c

Casey Brooks

07/24/2019, 9:06 PM
I think you’ll be able to assign
this
to a variable in the initial
customDsl
scope, and then refer to that object from child scopes. The
DslMarkers
should only limit the implicit scope, but explicit scope (like variabled declared in that scope) should still be accessible
Copy code
customDsl {
    val globalScope = this
    
    scopedEntry { /*work*/ }
    nestedObject {
        // this should be allowed because I want it global
        globalScope.globalHelper { /*work*/ }
    }
    globalHelper { /*work*/ }
}
b

Brandon Ward

07/24/2019, 9:09 PM
That's not horrible, but still no worse than just needing to call
this@customDsl.globalHelper { /* work */ }
wherever the helper is needed outside the original scope
d

Dominaezzz

07/24/2019, 9:56 PM
You can specifically mark the lamdba parameter with
@CustomDslMarker
.
fun scopedEntry(@CustomDslMarker init: Any.() -> Unit)
b

Brandon Ward

07/24/2019, 9:57 PM
Yeah, I tried that, which allowed my global function to be called globally, but didn't restrict the other scoped functions
I should clarify, it helps a little, some things get properly scoped, but not everything, so I guess it's better than nothing.
3 Views