<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Den B</title>
    <description>The latest articles on DEV Community by Den B (@den_b_7509ed8a6d395614f45).</description>
    <link>https://dev.to/den_b_7509ed8a6d395614f45</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3001039%2F7e2c0b29-2f46-42e4-addf-ef143122c84f.png</url>
      <title>DEV Community: Den B</title>
      <link>https://dev.to/den_b_7509ed8a6d395614f45</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/den_b_7509ed8a6d395614f45"/>
    <language>en</language>
    <item>
      <title>Advanced Kotlin DSL: Building Smart Builders with Validation and Error Aggregation</title>
      <dc:creator>Den B</dc:creator>
      <pubDate>Tue, 01 Apr 2025 09:03:27 +0000</pubDate>
      <link>https://dev.to/den_b_7509ed8a6d395614f45/advanced-kotlin-dsl-building-smart-builders-with-validation-and-error-aggregation-4ech</link>
      <guid>https://dev.to/den_b_7509ed8a6d395614f45/advanced-kotlin-dsl-building-smart-builders-with-validation-and-error-aggregation-4ech</guid>
      <description>&lt;p&gt;Kotlin DSLs are often used for configuration or UI building, but they rarely enforce correctness at compile or runtime. You can enhance DSLs with declarative validation logic, using internal builder patterns to collect multiple errors at once—an uncommon but highly powerful pattern.&lt;/p&gt;

&lt;p&gt;✅ Use Case: Validating a DSL for Form Submission Logic&lt;/p&gt;

&lt;p&gt;Instead of failing fast on the first error, we want to collect all configuration errors and then fail with full diagnostics.&lt;/p&gt;

&lt;p&gt;⚙️ DSL with Validation Support&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@DslMarker
annotation class FormDsl

@FormDsl
class FormBuilder {
    private val errors = mutableListOf&amp;lt;String&amp;gt;()
    private var name: String? = null
    private var email: String? = null

    fun name(value: String) {
        if (value.isBlank()) errors += "Name cannot be blank"
        name = value
    }

    fun email(value: String) {
        if (!value.contains("@")) errors += "Invalid email format"
        email = value
    }

    fun build(): Form {
        if (errors.isNotEmpty()) {
            throw IllegalArgumentException("Form validation failed:\n" + errors.joinToString("\n"))
        }
        return Form(name!!, email!!)
    }
}

data class Form(val name: String, val email: String)

fun form(block: FormBuilder.() -&amp;gt; Unit): Form =
    FormBuilder().apply(block).build()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ Usage:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;val form = form {
    name("")
    email("no-at-symbol")
}
// Throws with detailed error report:
// - Name cannot be blank
// - Invalid email format
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;📌 Notes for Quick Reference&lt;br&gt;
    • ✅ Use errors: MutableList to accumulate configuration mistakes.&lt;br&gt;
    • ✅ Use @DslMarker to prevent scope collisions.&lt;br&gt;
    • ❌ Avoid throwing inside individual DSL methods—defer until .build().&lt;br&gt;
    • ✅ Ideal for CI/CD DSLs, Terraform-style Kotlin config, or complex form builders.&lt;/p&gt;

&lt;p&gt;🚀 Pro Tip: Add warning() support next to error() to flag non-blocking suggestions during DSL execution, mimicking compiler diagnostics.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>kotlin</category>
      <category>dsl</category>
    </item>
    <item>
      <title>Advanced Kotlin: Inline Reified Generics for Type-Safe Reflection and JSON Parsing</title>
      <dc:creator>Den B</dc:creator>
      <pubDate>Tue, 01 Apr 2025 07:15:43 +0000</pubDate>
      <link>https://dev.to/den_b_7509ed8a6d395614f45/advanced-kotlin-inline-reified-generics-for-type-safe-reflection-and-json-parsing-1j2m</link>
      <guid>https://dev.to/den_b_7509ed8a6d395614f45/advanced-kotlin-inline-reified-generics-for-type-safe-reflection-and-json-parsing-1j2m</guid>
      <description>&lt;p&gt;One of the most powerful yet underused features in Kotlin is the combination of inline + reified type parameters. This allows you to retain type information at runtime, enabling type-safe operations like JSON deserialization, dependency resolution, and reflection—without needing explicit Class objects.&lt;/p&gt;

&lt;p&gt;✅ &lt;strong&gt;Real-World Problem: Generic JSON Parsing&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In Java/Kotlin with Gson or Moshi:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;inline fun &amp;lt;reified T&amp;gt; Gson.fromJson(json: String): T =
    this.fromJson(json, object : TypeToken&amp;lt;T&amp;gt;() {}.type)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🔍 Without reified, you’d have to pass Class explicitly, and lose type safety for generic types like List.&lt;/p&gt;

&lt;p&gt;💡 &lt;strong&gt;Use Case: DSL or DI Framework&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Suppose you’re writing a lightweight DI container or DSL:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;inline fun &amp;lt;reified T&amp;gt; inject(): T {
    return container.resolve(T::class.java) as T
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ No need to pass User::class.java — it’s inferred and reified at compile time.&lt;/p&gt;

&lt;p&gt;🔥 &lt;strong&gt;Use Case: Type-Safe Routing or Serialization&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;inline fun &amp;lt;reified T&amp;gt; isTypeOf(value: Any): Boolean {
    return value is T
}

val result = isTypeOf&amp;lt;String&amp;gt;("test") // ✅ true
val result2 = isTypeOf&amp;lt;List&amp;lt;Int&amp;gt;&amp;gt;(listOf(1, 2, 3)) // ✅ true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;📌 &lt;strong&gt;Notes for Quick Reference&lt;/strong&gt;&lt;br&gt;
    • Use inline + reified when you want access to T::class, is T, or TypeToken().&lt;br&gt;
    • Great for JSON parsing, reflection utilities, and type-safe DSLs.&lt;br&gt;
    • Avoid using it in public API boundaries that must be compiled to Java—Java won’t understand reified generics.&lt;/p&gt;

&lt;p&gt;🚀 &lt;strong&gt;Pro Tip:&lt;/strong&gt; When writing coroutine-based network libraries or internal frameworks, use inline reified to make API usage cleaner and more intuitive—especially when dealing with typed responses.&lt;/p&gt;

</description>
      <category>kotlin</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
