Marc Knaup
05/06/2020, 4:55 AMinvoke
or put it into parenthesis. Neither make for a good DSL.
At the moment to work around that limitation I always have to add two functions:
• one that returns a lambda
• one that accepts a trailing lambda for immediate invocation and returns Unit
Ben Woodworth
05/06/2020, 5:15 AM(test(1)) {
println(it)
}
Marc Knaup
05/06/2020, 6:41 AMraulraja
05/06/2020, 12:04 PMraulraja
05/06/2020, 12:04 PMclass test(val value: Int, val f: (Int) -> Unit) {
init {
f(value * 2)
}
}
fun main() {
test(1) {
println(it)
}
}
raulraja
05/06/2020, 12:04 PMMarc Knaup
05/07/2020, 10:40 AMtest
functions: one that returns something invokable and one that directly takes and invokes the lambda.
If you have a lot of cases like that (and I have plenty) it adds to a lot of unnecessary and repetitive code.elizarov
05/08/2020, 11:43 AMMarc Knaup
05/08/2020, 3:09 PMsourceSets.named("main").resources.srcDir("resources")
sourceSets {
named("main") {
resources {
srcDir("resources")
}
}
}
Module creators create configurable components and write related DSL by providing for example
val ComponentSet<Project>.sourceSets: ComponentSet<SourceSet>
…
fun ComponentSet<SourceSet>.named(name: String): ComponentSet<SourceSet>
…
val ComponentSet<SourceSet>.resources: ComponentSet<SourceDirectorySet>
…
fun ComponentSet<SourceDirectorySet>.srcDir(path: String)
Everything is configured lazily using ComponentSet
which will configure 0 or more components of the specified type.
ComponentSet<Component>
has an operator fun invoke(configure: ComponentSet<Component>.() -> Unit)
to lazily apply a configuration to the set of components.
Properties and functions return such a ComponentSet
and DSL users can decide whether they want to chain (a.b.c { … }
) or nest (a { b { c { … }}}
) depending on their need/use case.
I want component writers to not have to duplicate every single configuration function (one lambda val
and one fun
that takes lambda) but only write each property/function exactly once.Marc Knaup
05/09/2020, 11:43 AMMarc Knaup
05/09/2020, 12:33 PM