Chapter 5
Manage Scope and Handle Errors

For a language to serve as a host for creating internal DSLs, it needs to have low ceremony, provide the ease to create highly fluent code, have the ability to implement implicit execution context, and have the capability to add domain-specific functions and properties to existing abstractions or data types. In addition to Kotlin, many other languages, such as Groovy and Ruby, have those capabilities and are great for serving as a host. A few things, however, make Kotlin a better choice.

A good design shouldn’t only make an application easier to work with; it should also fail fast and gracefully when things don’t go well. When designing DSLs, our design should alert users about syntax errors and also prevent them from making semantical errors that may otherwise be correct syntactically.

Due to the statically typed nature of Kotlin, our users will get syntax errors immediately, well before the execution of their script. In fact, the statically typed nature has led to the creation of helpful IDE support for both the language and the DSLs written in it. Developers and IDE users don’t run the risk of partial execution or processing. The reason for this nice behavior is that in Kotlin, scripts are compiled fully, just in time and in memory, before execution begins.

As we know, not all valid syntax is sensible semantically. Certain function calls or property access can be done only in some contexts and not in others. In Chapter 4, Provide Contexts, we saw how creating different contexts can help us to control access to some functions and properties. While that approach is helpful, Kotlin also provides a few other capabilities to take that effort further, for better results.

In this chapter, we’ll look at examples of errors in code and how to design so that those can be detected fast and handled gracefully.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.120.109