Is there a way to pass these references without ha...
# announcements
m
Is there a way to pass these references without having to repeat
User::
over and over again? 🤔
g
I actually dealt with that in a small library I made, take a look: https://github.com/gabrielshanahan/moroccode. The basic trick is to define lambdas with receivers (the receiver being the User object), then you can just use the field names themselves. Hope this helps
m
Thanks Gabriel, but I do need the
KProperty
and I don’t have an instance of
User
here. Thus I cannot call anything where
User
is a receiver parameter.
g
can you show me the signature of conversion<T>?
however, if you need the KProperty, I'm afraid there's not much you can do
m
Copy code
inline fun <reified Value : Any> conversion(noinline configure: Conversion<Value>.() -> Unit) =
			conversion(valueClass = Value::class, configure = configure)
g
Copy code
class Test(val a: Int, b: Int)

fun myFun(test: Test, f:Test.() -> Unit) {
    test.f()
}

myFun(Test(1, 2)) {
    println((::a).javaClass)
}
I tried out this real quick:
but got a "References to variables aren't supported yet" error. So..maybe someday 🙂
m
and you need an instance here 🙂
g
I didn't understand what you meant when you said that the first time, now I get that you're working with the actual KProperties themselves. Sorry I can't help you more
m
Thanks for trying though!
r
you can do it but intellij does not support it
Copy code
class A {
    val a = 1
    fun foo() = "a"
}

fun bar(f: A.() -> Unit) = 1

fun test() {
    bar { ::a }
}
m
@robstoll same problem here. I don’t have an instance of
A
.
r
ah right, upvote the following feature request 😉 https://youtrack.jetbrains.com/issue/KT-23777
m
Wouldn’t that make it worse in my case? 😄
7x
field(typeOf(???)::someProp)
That’s worse than repeating the name
r
😄
m
Basically we’d need to be able to scope into a type instead of a value.
Copy code
withType<User> {
   field(::birthday)
}
I guess that would change the language a lot 😅
e
Apply?
m
You can’t
apply()
on a class, only on an instance.
User.apply { … }
won’t work (or use the companion, which is not intended)
r
I would have imagined that
typeOf
give some kind of meta-structure for the corresponding type (just saw that typeOf now actually exists in Kotlin -> KType). However, it is dumb and you cannot do something like:
Copy code
fun bar(f: KType<A>.() -> Unit) = 1

@ExperimentalStdlibApi
fun test() {
    bar { ::a }(typeOf(A))
}
m
That looks super confusing 😅
r
ok, for you, to lower the confusion 😉
Copy code
with(typeOf(A)){
    ::a
}
which is what you wanted no?
m
Kinda, yeah.
KType
isn’t generic though :)
And
::a
potentially ambiguous now. Is it a property of
KType
or of the type that
KType
is referring to? ^^
r
you are right 🙂 that's something which would need to be specified. easiest would be if you can do
typeOf(A).a
which returns
KProperty<Int>
but yeah.. this does not exist so it does not really make sense to dream about it, right?
m
Yeah, unlikly that it’ll be valuable enough so that it’s worth introducing a lot of new complexity.
t
Hmm, maybe you can use contracts?
On second tought, I don't think contracts would be useful here. Tbh, it seems like this is out of reach at least for now