I can get this to compile:
class Example {
val myLambda = ::addExt
val lambdaMap : Map<String, (String) -> String> = mapOf(
"world" to { value -> addExt(value) }
)
fun addExt(value: String): String {
return "$value.gif"
}
}
But, I can’t seem to get this work:
class Example {
val myLambda = ::addExt
val lambdaMap: Map<String, (String) -> String> = mapOf(
"world" to { value -> addExt(value) },
"mars" to ::addExt
)
fun addExt(value: String): String {
return "$value.gif"
}
}
Oddly, it’s the line with “world” that is the problem. If I comment it out, it will compile.
I get the error:
Type mismatch.
Required:
KFunction1<@ParameterName String, String>
Found:
(???) → String
If I change it to:
class Example {
val myLambda = ::addExt
val lambdaMap: Map<String, (String) -> String> = mapOf(
"world" to { value:String -> addExt(value) },
"mars" to ::addExt
)
fun addExt(value: String): String {
return "$value.gif"
}
}
With the explicit, “value: String”, it will compile again.
I would expect Kotlin to implicitly figure out the return type as it did in the first example.
Bug or not a Bug?