<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Annotations Archives - Codersee blog- Kotlin on the backend</title>
	<atom:link href="https://blog.codersee.com/tag/annotations/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>Kotlin &#38; Backend Tutorials - Learn Through Practice.</description>
	<lastBuildDate>Wed, 16 Apr 2025 04:49:51 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://blog.codersee.com/wp-content/uploads/2025/04/cropped-codersee_logo_circle_2-32x32.png</url>
	<title>Annotations Archives - Codersee blog- Kotlin on the backend</title>
	<link></link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Scope Control With @DslMarker Annotation. Kotlin DSLs.</title>
		<link>https://blog.codersee.com/scope-control-dslmarker-kotlin-dsl/</link>
					<comments>https://blog.codersee.com/scope-control-dslmarker-kotlin-dsl/#respond</comments>
		
		<dc:creator><![CDATA[Piotr]]></dc:creator>
		<pubDate>Tue, 07 Nov 2023 13:35:19 +0000</pubDate>
				<category><![CDATA[Kotlin]]></category>
		<category><![CDATA[Annotations]]></category>
		<category><![CDATA[Core Kotlin]]></category>
		<category><![CDATA[Kotlin DSL]]></category>
		<guid isPermaLink="false">https://codersee.com/?p=9008328</guid>

					<description><![CDATA[<p>This time, I will show you how to control scope with @DslMarker annotation when creating your DSLs in Kotlin.</p>
<p>The post <a href="https://blog.codersee.com/scope-control-dslmarker-kotlin-dsl/">Scope Control With @DslMarker Annotation. Kotlin DSLs.</a> appeared first on <a href="https://blog.codersee.com">Codersee blog- Kotlin on the backend</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">Hello and welcome to the next lesson! This time, I will show you how to <strong>control scope with @DslMarker</strong> annotation when creating your DSLs in Kotlin. </p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p class="wp-block-paragraph"></p>
<cite>Note: this article is based on my Complete <a href="https://codersee.com/the-complete-kotlin-course/">Kotlin Course</a> lesson, which I highly encourage you to check out if you are looking for a comprehensive Kotlin guide. </cite></blockquote>



<p class="wp-block-paragraph">If this is your first meeting with DSLs, then I would recommend you to check out my other article, in which I explain and show how to <a href="https://blog.codersee.com/kotlin-type-safe-builders-make-your-custom-dsl/">implement Kotlin DSL</a> step-by-step.</p>



<h2 class="wp-block-heading" id="h-video-tutorial">Video Tutorial</h2>



<p class="wp-block-paragraph">If you prefer <strong>video content</strong>, then check out my video:</p>



<div style="text-align: center; width: 90%; margin-left: 5%;">
<a href="https://blog.codersee.com/scope-control-dslmarker-kotlin-dsl/"><img decoding="async" src="https://blog.codersee.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=%2F%2Fi.ytimg.com%2Fvi%2FuItQGNnbUXo%2Fhqdefault.jpg" alt="YouTube Video"></a><br /><br /></p></div>



<p class="wp-block-paragraph">If you find this content useful,<strong> please leave a subscription&nbsp;</strong> 😉</p>



<h2 class="wp-block-heading" id="h-uncontrolled-scope">Uncontrolled Scope</h2>



<p class="wp-block-paragraph">But before we dive into the solution with Kotlin @DslMarker, let&#8217;s understand what problem it solves first. </p>



<p class="wp-block-paragraph">So as the first step, let&#8217;s prepare a simple DSL with <code>Board</code>, <code>Task</code>, <code>Author</code>, and <code>Comment</code> classes:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">enum class BoardColor {
  BLACK, WHITE, GREEN, BLUE
}

class Board {
  var title: String = ""
  var color: BoardColor = BoardColor.BLUE
  val tasks: MutableList&lt;Task> = mutableListOf()

  fun task(init: Task.() -> Unit) {
    val task = Task().apply(init)
    tasks.add(task)
  }
}

class Task {
  var title: String = ""
  var description: String = ""
  val comments: MutableList&lt;Comment> = mutableListOf()

  fun comment(init: Comment.() -> Unit) {
    val comment = Comment().apply(init)
    comments.add(comment)
  }
}

class Comment {
  var comment: String = ""
  var author: Author = Author()

  fun author(init: Author.() -> Unit) {
    val author = Author().apply(init)
    this.author = author
  }
}

class Author {
  var name: String = ""
}

fun board(init: Board.() -> Unit): Board =
  Board()
    .apply(init)</pre>



<p class="wp-block-paragraph">With the following Kotlin DSL implementation, we would expect that we could introduce different Boards, with Tasks inside them, which would have some Comments written by Authors. </p>



<p class="wp-block-paragraph">But let&#8217;s take a look at the following code: </p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">fun main() {
  val board = board {
    task {
      task {
        task {
          comment {
            task { }
            author {
              task { }
              comment {
                task { }
              }
            }
          }
        }
      }
    }
  }
}</pre>



<p class="wp-block-paragraph">Will it compile? <strong>Unfortunately, yes</strong>.</p>



<p class="wp-block-paragraph">We invoke a <em>task</em> within a <em>task</em>, within a <em>task</em>, and so on. Moreover, we can invoke the <em>task</em>, or <em>comment</em> functions inside the <em>author</em>.&nbsp;</p>



<p class="wp-block-paragraph">And that&#8217;s because, <strong>by default, we can call the methods of every available receive</strong>r, which can lead to such situations.&nbsp;</p>



<h2 class="wp-block-heading" id="h-kotlin-dslmarker-to-the-rescue">Kotlin @DslMarker To The Rescue</h2>



<p class="wp-block-paragraph">At this point, we already know what the issue is and what we will use to fix it. </p>



<p class="wp-block-paragraph">But what exactly does the @DslMarker do in Kotlin? </p>



<p class="wp-block-paragraph">Well, in practice, we use this annotation to introduce our new custom annotations. Then, we make use of them to mark classes and receivers, thus preventing receivers marked with the same annotation from being accessed inside one another.</p>



<p class="wp-block-paragraph">This way, we won’t be able to access members of the outer receiver (like the&nbsp;<em>task&nbsp;</em>function, which is declared inside the&nbsp;<em>Board</em> class from the <em>Task</em> class instances).</p>



<p class="wp-block-paragraph">Let&#8217;s consider the updated example: </p>



<pre class="EnlighterJSRAW" data-enlighter-language="kotlin" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">@DslMarker
annotation class BoardDsl

enum class BoardColor {
  BLACK, WHITE, GREEN, BLUE
}

@BoardDsl
class Board {
  var title: String = ""
  var color: BoardColor = BoardColor.BLUE
  val tasks: MutableList&lt;Task> = mutableListOf()

  fun task(init: Task.() -> Unit) {
    val task = Task().apply(init)
    tasks.add(task)
  }
}

@BoardDsl
class Task {
  var title: String = ""
  var description: String = ""
  val comments: MutableList&lt;Comment> = mutableListOf()

  fun comment(init: Comment.() -> Unit) {
    val comment = Comment().apply(init)
    comments.add(comment)
  }
}

@BoardDsl
class Comment {
  var comment: String = ""
  var author: Author = Author()

  fun author(init: Author.() -> Unit) {
    val author = Author().apply(init)
    this.author = author
  }
}

@BoardDsl
class Author {
  var name: String = ""
}

fun board(init: Board.() -> Unit): Board =
  Board()
    .apply(init)

fun main() {
  val board = board {
    task {  // OK
      task {  // Does not compile
        task {  // Does not compile
          comment {  // OK
            task { }  // Does not compile
            author {  // OK
              task { }  // Does not compile
              comment {  // Does not compile
                task { }  // Does not compile
              }
            }
          }
        }
      }
    }
  }
}</pre>



<p class="wp-block-paragraph">Firstly, we introduce the <code>@BoardDsl</code> annotation, which uses the <code>@DslMarker</code>. </p>



<p class="wp-block-paragraph">Following, we must annotate every class in our hierarchy with our new annotation- this way, we make the Kotlin compiler &#8220;aware&#8221; of the hierarchy in our DSL. </p>



<p class="wp-block-paragraph">Lastly, we can clearly see that the code <strong>will not compile</strong> whenever we try to nest the unwanted type inside another. </p>



<h2 class="wp-block-heading" id="h-scope-control-with-kotlin-dslmarker-summary">Scope Control With Kotlin @DslMarker Summary</h2>



<p class="wp-block-paragraph">Basically, that&#8217;s all for this tutorial on <strong>how to control scope</strong> when implementing a <strong>Kotlin DSL</strong> using the <strong>@DslMarker </strong>summary.</p>



<p class="wp-block-paragraph">If you enjoy such a short, practice-focused learning approach then do not forget to check out my course and let me know in the comments section. </p>



<p class="wp-block-paragraph">If you&#8217;d like to learn a bit more about the annotation itself, then the <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-dsl-marker/" target="_blank" rel="noreferrer noopener">documentation</a> may be useful to you too. </p>
<p>The post <a href="https://blog.codersee.com/scope-control-dslmarker-kotlin-dsl/">Scope Control With @DslMarker Annotation. Kotlin DSLs.</a> appeared first on <a href="https://blog.codersee.com">Codersee blog- Kotlin on the backend</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.codersee.com/scope-control-dslmarker-kotlin-dsl/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 

Served from: blog.codersee.com @ 2026-05-24 20:09:59 by W3 Total Cache
-->