is it possible to reference an object's member as ...
# announcements
l
is it possible to reference an object's member as a kproperty1? rn if i reference a object's member i get a KProperty0 but for my use case it'd be nice if I could use a KProperty1 so i can bound the receiver.
a
If you get a reference to a property on a class or interface, it will be a KProperty1; on an object it will be a KProperty0. So in this case, you probably want to replace the object in the reference with the class/interface specifying the property
d
Does you mean an
object
or an instance of a
class
? If you mean the former ... then it's not really possible (at least conceptually speaking).
l
thanks for responding. lets go with this function
Copy code
fun <T> Foo<T>.values(vararg values: KProperty1<T, *>) { ... }
In the Foo class it has an instance of T, and could get the values of the kproperties. With this method I know the values are from T and I'm happy with this. However if I use a KProperty0<*> e.g., from an object, I lose the guarantee that the property is from T.
Hmm, alright, that's unfortunate. Didn't really think it would be possible, but wasn't complete sure. thanks for the clarification
d
Oh, I'm guessing you have
object UnknownName : T { ... }
.
You'll need an interface or similar as Steve has suggested.
l
heres a barebones snippet
Copy code
class SelectOp<D>(val entity: Entity<D>): Operation

class SelectValuesOp<D>(val entity: Entity<D>, val values: List<Column<*>>): Operation

fun <D> SelectOp<D>.values(vararg values: KProperty1<Entity<D>, Column<*>>) {
  ...
}

//user code
object SomeEntity: Entity<SomeEntityData>() {
  val foo = Column<Int>()
}

data class SomeEntityData(val foo: Int)
in this situation, id like the user to be able to do
Copy code
SelectOp(SomeEntity).values(SomeEntity::foo)
how would i go about using an interface/class to specify properties that are specified by the user?