Reputation: 1177
I have a class -
class ES {
var issue: SomeClass? = null
}
I need to access its getter in another class -
class CSS {
private val ref: Supplier<SomeClass?> = ES::issue
}
However this is not working. Its throws this error -
Type mismatch.
Required: Supplier<SomeClass?>
Found : KMutableProperty1<ES, SomeClass?>
Can someone tell me what am I doing wrong? I am actually in the process of converting java code to kotlin.
UPDATE
I need a static reference to the getter of the ES class, similar to JAVA, where we can do ->
Function<ES, SomeClass> ref = ES::getIssue;
Upvotes: 0
Views: 399
Reputation: 93629
In Kotlin, instead of using Supplier, you use functional syntax for the type. In this case, the equivalent of your Supplier<SomeClass?>
would be () -> SomeClass?
(assuming ES is an object
since that's how you used it in your example code):
class CSS(/*...*/) {
private val ref: () -> SomeClass? = ES::issue
}
But if you have to use Supplier specifically so it can be easily used with Java code, you can wrap your getter reference in a Supplier implementation:
class CSS(/*...*/) {
private val ref: Supplier<SomeClass?> = Supplier(ES::issue)
}
Update
If you want the getter without a specific instance of the class, similar to Function<ES, SomeClass>
in Java, then you need to make ES
a parameter of the function.
Either
private val ref: (ES) -> SomeClass? = ES::issue
or
private val ref: ES.() -> SomeClass? = ES::issue
I don't believe there's a way to do this with Supplier, but I don't think you could do it in Java with Supplier either.
Upvotes: 1