Any way to simplify this by getting rid of `fun St...
# codereview
j
Any way to simplify this by getting rid of
fun String.()
for each lamda inside this map creation?
Copy code
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
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:
Copy code
mapOf(
  "One" to fun String.() = println(this)
)
j
Thanks @Shawn. Guess I'll just have to pass in the lambda receiver type as an argument for now then.