I am trying to create a list of lambdas in Kotlin, but am getting the following compile-time error:
Destructuring declaration initializer of type Int must have a 'component1()' function
Here is my code:
val pathXRanges = listOf<(Int) -> List<Int>>(
{(extX) -> ((extX - (board.k - 1))..(extX + board.k - 1)).toList()}, // diagonal 1 (y = -x)
{(extX) -> ((extX - (board.k - 1))..(extX + board.k - 1)).toList()}, // diagonal 2 (y = x)
{(extX) -> List(board.k * 2 - 2) { extX } }, // x = x
{(extX) -> ((extX - (board.k - 1))..(extX + board.k - 1)).toList()} // y = y
)
That's pretty much it! Any help is appreciated, let me know if you need clarification on anything.
Thanks!
Simply remove the brackets from the extX lambda parameter values i.e.:
val pathXRanges = listOf<(Int) -> List<Int>>(
{ extX -> ((extX - (board.k - 1))..(extX + board.k - 1)).toList() }, // diagonal 1 (y = -x)
{ extX -> ((extX - (board.k - 1))..(extX + board.k - 1)).toList() }, // diagonal 2 (y = x)
{ extX -> List(board.k * 2 - 2) { extX } }, // x = x
{ extX -> ((extX - (board.k - 1))..(extX + board.k - 1)).toList() } // y = y
)
The brackets indicate to the compiler that the lambda parameter should be destructured, and the parameter's component1() operator function should be called to assign a value to extX.
Of course, Int does not have a component1() function, hence the error:
Destructuring declaration initializer of type Int must have a 'component1()' function`.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With