Hello. I am having an issue with destructuring declarations in lambdas when compiled to javascript. To clarify:
Copy code
val lam = { (a, b) ->
println("$a, $b")
}
lam(Pair(5, 3))
prints
5, undefined
But:
Copy code
val lam = { pair ->
val a = pair.first
val b = pair.second
println("$a, $b")
}
lam(Pair(5, 3))
prints
5, 3
a
anton.bannykh
11/12/2019, 10:10 AM
What is the type of
lam
? Neither snippets should compile in isolation, because the compiler cannot infer the parameter types.
The following should work though:
Copy code
val lam: (Pair<Int, Int>) -> Unit = { (a, b) ->
println("$a, $b")
}
lam(Pair(5, 3))
Could you please try
Copy code
val lam = { pair ->
val a = pair.component1()
val b = pair.component2()
println("$a, $b")
}
lam(Pair(5, 3))
If it fails then destructuring is not the issue here. If it work
anton.bannykh
11/12/2019, 10:11 AM
If the snippets you've provided do compile on your side, please let know how. Maybe I'm missing something.
z
zceejkr
11/12/2019, 10:21 AM
I never tried the compile the stuff I wrote, since i tried to destil the essence. In my code I am passing the lambda as an argument to another function, and I do not think the type inference is the problem since InteliJ says the snippet is OK. When I have time I will try to write an actual test that also compiles to the JVM to compare.
a
anton.bannykh
11/14/2019, 4:13 PM
Well, I can only advise to check the
componentN
variant, because that's what destructuring delegates to. If it fails it's much easier to debug. If it doesn't, then it might be a compiler issue.