Narrowing Access with Scope Control
You’ve seen how to create internal DSLs in Kotlin. Unlike external DSLs, internal DSLs have the benefit of riding on a host language, and we don’t have to implement parsers. That removes a lot of effort on our part as DSL designers. However, anything that’s possible in the host language, like property access or calls to arbitrary functions and methods in scope are all permitted within the DSL code. That freedom may be too much at times. Kotlin makes an effort to narrow that access a little with a scope control annotation.
In spite of the facility Kotlin offers, there’s no way to tell the compiler to reject calls to a top-level function or an access to a variable that’s in lexical scope. But, with a special ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access