October 2019
Intermediate to advanced
434 pages
11h 54m
English
Currently, we have no @DslMarker annotation in place so, as we start to nest these function calls, we have access to functions for multiple different receivers. For example, in the following code snippet, we can call the soda function within the init block for Pizza:
+HawaiianPizza { +Pepperoni soda(Coke) // want to avoid this}
This behavior is not ideal because it doesn't make sense to add a Soda to Pizza. So, by default, we want to limit this behavior. To do that, we can create a new @DslMarker annotation:
@DslMarkerannotation class ItemTagMarker
We can then add that to the base class for our Items:
@ItemTagMarkerabstract class Item(val name: String)
With that annotation in place, the compiler will mark it as an error ...
Read now
Unlock full access