https://kotlinlang.org logo
#compiler
Title
# compiler
s

Sam

07/29/2022, 7:58 AM
When I have a function that expects a value of type
(A) -> B
, it’d be cool to be able to just pass
::foo
instead of
A::foo
. Is there a name for what I want? Even better, is there an existing ticket/proposal for it? Example:
Copy code
data class A(val name: String)
fun <B> A.getProp(prop: (A) -> B) = prop(this)
fun A.getName() = getProp(::name) // I want this
fun A.getName2() = getProp(A::name) // But I have to do this instead
2
The context where this bugs me is in tests using
assertk
, where I end up with things like:
Copy code
assertThat(actual)
    .isInstanceOf(ExpectedClass::class).all {
        prop(ExpectedClass::a).isEqualTo(expectedA)
        prop(ExpectedClass::b).isEqualTo(expectedB)
    }
I end up repeating the
ExpectedClass
over and over again
But I keep doing it because it is useful to use the property reference rather than a lambda (like
prop { it. a }
), so that when the assertion fails it will include the name of the property
c

Chrimaeon

07/29/2022, 8:18 AM
Better post in #language-proposals
s

Sam

08/04/2022, 7:48 AM
👍 thanks for the suggestion, will do!
3 Views