https://kotlinlang.org logo
#codereview
Title
# codereview
b

bodiam

09/01/2021, 12:32 PM
Hi all, I’ve created a small DSL, but my challenge is that I need a reference to some of the items in the tree I’m building, and I was wondering if you have any suggestions on how to do this. This is my current DSL:
Copy code
lateinit var childFolder1 : FolderNode
         lateinit var childFolder2 : FolderNode

         val rootFolder = buildFolders(am,"System") {
            childFolder1 = folder("A") {
                +entry(Attribute("x"), Sample("1"))
            }
            childFolder2 = folder("A") {
                +entry(Attribute("x"), Sample("2"))
                +entry(Attribute("x"), Sample("2"))
            }

            +childFolder1
            +childFolder2
        }

        println(childFolder1) // simplified here
As you can see, I need a reference to childFolder1 in my code, but my current solution seems a bit non-idiomatic, so I was wondering if you have any suggestions on how to improve this? (I’m also not sure if this is the right channel, the new setup confuses me a bit, so if I need to post this somewhere else, please let me know)
p

Paul Griffith

09/01/2021, 2:36 PM
I think the only way I've seen 'deferral' like this happen is through delegation (the
by
keyword), but having tried to implement almost exactly this problem myself, I was never able to come up with a very satisfactory solution. I don't know if there is a way to do this and maintain compile-time safety, but I'd be happy to be wrong 🙂
b

bodiam

09/02/2021, 1:06 AM
Thanks for the response Paul. I’ll have a look at the
by
, see if I can get someplace with that.
3 Views