Hi guys. I'm trying to write a DSL. Suppose I have...
# announcements
u
Hi guys. I'm trying to write a DSL. Suppose I have this structure:
Copy code
class A(val someObject : SomeObjectClass) {

    fun doSomethingWithSomeObject() {
        // TODO do something with someObject
    }
}
How would I achieve something like the following in Kotlin:
Copy code
coolDSL(someObject) {

    doSomethingWithSomeObject {
        someObject.callSomeMethod()
    }

}
Thanks in advance.
u
@Denis A, Thanks. As far as I understand, apply() will call appropriate functions on someObject. I would like not call functions, but provide implementations.
g
You need something like this
Copy code
fun cooldDsl(
   obj: SomeObjectClass, 
   block: SomeObjectClass.() -> Unit
) {
  block(obj)
}
d
I think, we can’t change base syntax (of any language), but we can use own codestyle
g
Just need one more wrapper to have doSomethingWithSomeObject
If you provide more complete example I can show how to wrap it to dsl
u