e.g. ``` thing { a = "test" b = "wat" } ``...
# announcements
j
e.g.
Copy code
thing {
    a = "test"
    b = "wat"
}
d
In this case you'd want
thing
to have signature
fun thing(body: MyBuilder.() -> Unit)
where
MyBuilder
has properties
a
and
b
.
j
If I knew it would have the properties a and b that would be fine, but I don't know that
This is where atoms would be really useful 😕
s
You could define an infix function that automatically adds the declaration to a collection in the builder
but you wouldnt be able to use '='
j
I could get away with
Copy code
thing {
    it[:a] = "wat"
}
Id prefer not to refer to strings all the way thru
s
Copy code
fun buildAThing() {
    thing {
        "a" equals "test"
        "b" equals "wat"
    }
}

fun thing(function: Builder.() -> Unit) {
    Builder().function()
}

class Builder {

    val declarations = mutableListOf<Declaration>()

    infix fun String.equals(other: String): Declaration = Declaration(Term(this), Value(other)).apply { declarations.add(this) }
}

class Term(val name: String)
class Value(val value: String)

class Declaration(term: Term, value: Value)
j
Yea I knew that that was a possibility. But I wanted the DSL to be as typesafe as possible.
s
Can reified generics help?
j
Not sure how here
d
So, what determines which properties are ok? Are all properties ok?
j
any valid kotlin identifier is an okay property and any primative is an ok value
d
How do you expect type safety then? 😄
You kinda need just a
Map<String, Any?>
.