https://kotlinlang.org logo
#getting-started
Title
# getting-started
c

Colton Idle

08/17/2020, 7:36 PM
I have a java method written as such
Copy code
static Function1<MyManager, String> doTheManaging(){
    return MyManager::condense;
}
The kotlin converter can't seem to convert this. Can anyone point me in the right direction? Converter gives me a new companion object then
Copy code
fun doTheManaging(): Function1<MyManager, String> {
 return { valueMap: Map<String, Any> -> condense(valueMap)}
}
but the entire return statement is red
n

nanodeath

08/17/2020, 8:34 PM
try putting
Function1<MyManager, String>
right after the
return
also...Map<String, Any> != MyManager.
also if Function1 here is Kotlin's own Function1, you can just say
(MyManager) -> String
instead.
c

Colton Idle

08/17/2020, 9:04 PM
@nanodeath so if I'm understanding correctly, would you say that this is the correct conversion?
Copy code
fun doTheManaging(): Function1<MyManager, String> {
 return { valueMap: MyManager -> valueMap.condense()}
}
n

nanodeath

08/17/2020, 9:06 PM
assuming
condense()
returns a String...should work. what is Function1 exactly?
c

Colton Idle

08/17/2020, 9:06 PM
Sorry for asking this seemingly trivial question. Just the whole function stuff is a bit confusing to me. I always use the converter and never had these types of issues before.
condense does return a string!
n

nanodeath

08/17/2020, 9:07 PM
the best thing might be actually
Copy code
fun doTheManaging(valueMap: MyManager): String = valueMap.condense()
and then use a method reference
but if a method reference isn't possible,
Copy code
fun doTheManaging(): (MyManager) -> String =
    { valueMap: MyManager -> valueMap.condense }
should also do it, but...it admittedly a little weirder.
c

Colton Idle

08/17/2020, 9:10 PM
Okay. Thanks so much for your help. I think this is starting to make sense now.
n

nanodeath

08/17/2020, 9:11 PM
👍 the converter is usually pretty good, but...yeah, seems like it failed hard this time.
2 Views