DslMarker

  • Restricts scope
  • @DslMarker
    annotation class MyDsl() 
    
    

fun c1(a1: Class1.() -> Unit) : Class1 { 
  return Class1().apply(a1)
}

@MyDsl
class Class1(
    var a2: Class2? = null
    var prop1: String? = null) {

  fun c2(c2: Class2.() -> Unit) { 
    a2 = Class2().apply(a2)
  }
}

@MyDsl
class Class2( var prop2: String? = null) 

c1 { 
  c2 { 
    prop2 = "value"
    this@c1.prop1 = "value"
  }
}
Back Home Next