https://kotlinlang.org logo
Title
e

elect

10/03/2021, 7:40 AM
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

CLOVIS

10/03/2021, 8:03 AM
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

ephemient

10/03/2021, 8:39 AM
if
configure
isn't in scope then it's understandable,
val fn = input.configure
fn(input)
==
input.configure(input)
but it seems that you can avoid that just by bringing it into scope
c

CLOVIS

10/03/2021, 11:31 AM
input.configure()
and
configure(input)
are allowed, not
input.configure(input)
(you can't specify the same parameter twice)
e

elect

10/03/2021, 11:40 AM
no,
configure
is an Input property
class Input {
        var configure: Input.() -> Unit = {}
Ideally, I'd have
configure
as
() -> Unit
, but then I cant assign
block
to it
// 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
val fn = input.configure
input.fn()
j

Joffrey

10/03/2021, 12:30 PM
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
:
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

elect

10/04/2021, 9:22 AM
just wip