https://kotlinlang.org logo
Title
m

martmists

05/15/2022, 11:44 PM
Is something like the following possible somehow?
class BaseType {
    val someProp: Int = 10
}

fun handleProperty(prop: KProperty1<BaseType, Int>) {
    // get BaseType instance here somehow
}

fun main() {
    val x = BaseType()
    handleProperty(x::someProp)
}
e

ephemient

05/16/2022, 12:52 AM
KProperty1
is an unbound member property, so it doesn't have an instance. if you bind it to an instance, you get a
KProperty0
, but that doesn't expose the instance -
KProperty0
can also be a top-level (non-member) property.
what are you trying to do? if you're writing a property delegate, the
this
ref is passed separately
m

martmists

05/18/2022, 9:05 AM
Basically I currently have a setup like this:
class X {
    val in1 by input()
    val out1 by output()

    fun connect(output: String, node: X, input: String) { ... }
}

fun main() {
    x1 = X()
    x2 = X()
    x1.connect("out1", x2, "in1")
}

// ideally:

fun main() {
    x1 = X()
    x2 = X()
    x1::out1 connectsTo x2::in1  // connectsTo is an infix fun taking both properties but has access to the property owners
}