0
0
Kotlinprogramming~30 mins

DSL scope control with @DslMarker in Kotlin - Mini Project: Build & Apply

Choose your learning style9 modes available
DSL Scope Control with @DslMarker in Kotlin
📖 Scenario: Imagine you are building a simple Kotlin DSL (Domain Specific Language) to create a menu for a restaurant. You want to organize the menu into sections like Starters and Main Courses. To avoid confusion when adding items to different sections, you will use Kotlin's @DslMarker annotation to control the scope and prevent mistakes.
🎯 Goal: You will build a Kotlin DSL that lets you define a menu with sections and items. You will use @DslMarker to keep the scopes clear so that you cannot accidentally add a menu item to the wrong section.
📋 What You'll Learn
Create a @DslMarker annotation called MenuDsl
Define classes Menu, Section, and Item with DSL builder functions
Use @MenuDsl on the DSL classes to control scope
Build a menu with two sections: Starters and Main Courses
Add items to each section using the DSL
Print the menu showing sections and their items
💡 Why This Matters
🌍 Real World
DSLs help create readable and easy-to-write code for specific tasks like building menus, UI layouts, or configuration files.
💼 Career
Understanding Kotlin DSLs and scope control with <code>@DslMarker</code> is useful for Android developers and Kotlin backend developers who build custom DSLs or use libraries with DSLs.
Progress0 / 4 steps
1
Create the DSL marker and data classes
Create a @DslMarker annotation called MenuDsl. Then create three classes: Menu, Section, and Item. Each class should have a name property of type String. The Menu class should have a mutable list of Section called sections. The Section class should have a mutable list of Item called items. Annotate all three classes with @MenuDsl.
Kotlin
Need a hint?

Start by defining the @DslMarker annotation. Then create the classes with the required properties and annotate them with @MenuDsl.

2
Add builder functions for Menu and Section
Add a function section inside the Menu class that takes a name: String and a lambda with receiver Section. Inside this function, create a new Section with the given name, apply the lambda to it, and add it to the sections list. Also add a function item inside the Section class that takes a name: String and creates an Item with that name, adding it to the items list.
Kotlin
Need a hint?

Inside Menu, write a section function that creates and adds a Section. Inside Section, write an item function that adds an Item.

3
Create the menu using the DSL
Create a variable menu by calling Menu("Dinner Menu") and use the section function to add two sections: "Starters" and "Main Courses". Inside the "Starters" section, add items "Soup" and "Salad". Inside the "Main Courses" section, add items "Steak" and "Pasta".
Kotlin
Need a hint?

Create the menu variable using Menu("Dinner Menu") and add two sections with their items using the DSL functions.

4
Print the menu with sections and items
Write code to print the menu name, then for each section print its name, and for each item in the section print its name indented. Use a for loop with variables section and item to iterate over menu.sections and section.items. The output should look like this exactly: Dinner Menu Starters Soup Salad Main Courses Steak Pasta
Kotlin
Need a hint?

Print the menu name first. Then use nested for loops to print each section and its items with indentation.