I have a configuration lambda `var configure: Inp...
# getting-started
e
I have a configuration lambda
var configure: Input.() -> Unit
as a property on
Input
itself in order to execute it later on. However, to execute it externally, I have to call
input.configure(input)
... does this sound right? I'm inclined to think so, because if I scope
input
, then I can simply call configure as
input.apply { configure() }
, but I'd like to have some feedbacks on this
c
Signature:
Input.() -> Unit
Matching call:
input.configure()
Signature:
(Input) -> Unit
Matching call:
configure(input)
Signature:
Input.(Input) -> Unit
Matching call:
input.configure(input)
What is your case exactly? It seems like you are confusing some of these together
e
if
configure
isn't in scope then it's understandable,
Copy code
val fn = input.configure
fn(input)
==
input.configure(input)
but it seems that you can avoid that just by bringing it into scope
c
input.configure()
and
configure(input)
are allowed, not
input.configure(input)
(you can't specify the same parameter twice)
e
no,
configure
is an Input property
Copy code
class Input {
        var configure: Input.() -> Unit = {}
Ideally, I'd have
configure
as
() -> Unit
, but then I cant assign
block
to it
Copy code
// this is a companion object fun
fun Input(block: Input.() -> Unit) {
    val input = Input()
    input.configure = block
}
which sounds weird, because
configure
does have
Input
as receiver/host
val fn = input.configure
fn(input)
or
Copy code
val fn = input.configure
input.fn()
j
This seems super weird. Why is
configure
public here? It seems it's not supposed to be called from outside of the
Input
class, otherwise you could pass other instances of
Input
to it, and that would be quite a mess. I would suggest to create a public method inside
Input
that would simply call
configure
on
this
:
Copy code
class Input(
    private val configure: Input.() -> Unit
) {
    fun initialize() {
        this.configure()
    }
}
But this looks like an XY problem, I wonder what invariants you have for the input class. Is configure supposed to be called exactly once? When? It seems it should be used when creating the
Input
instance itself
e
just wip