https://kotlinlang.org logo
Title
j

jishindev

07/01/2019, 11:44 AM
Any way to simplify this by getting rid of
fun String.()
for each lamda inside this map creation?
mapOf<String, String.() -> Unit>(
            "One" to fun String.(){ println(this) }
        )
It gives
Type inference failed. Expected type mismatch: inferred type is Pair<String, () -> Unit> but Pair<String, String.() -> Unit> was expected
if I don't use the
fun String.()
while defining the lambda.
s

Shawn

07/01/2019, 12:15 PM
technically speaking you could just explicitly declare the parameter and type in the lambda:
{ str: String -> println(str)
, and that’d pass just fine as a
String.() -> Unit
, but to use
this
I don’t think you can avoid having to use the anonymous
fun
syntax
at very least you could use expression body syntax:
mapOf(
  "One" to fun String.() = println(this)
)
j

jishindev

07/01/2019, 12:28 PM
Thanks @Shawn. Guess I'll just have to pass in the lambda receiver type as an argument for now then.