Home > front end >  Not able to call function with two return variables
Not able to call function with two return variables

Time:01-30

Code snippet I'm new to Kotlin, I have successfully called a function returning two variables else where in my code but it give an error when used as shown. Everything worked until I decided I wanted to return two variables. I can get it to work if I use if then statements but that isn't as clean. Is there a way to do this using a lambda? It tells me the first value is expression unused and the second that a boolean is expected.

fun block (puzzle: Array<IntArray>, x:Int, y:Int): Pair<MutableList<Int>, MutableSet<Int>> {
    var blockNums: MutableList<Int> = mutableListOf()
    var blockNumsFinal: MutableSet<Int> = mutableSetOf()
    when {
        ((x in 0..2) and (y in 0..2)) -> (blockNums, blockNumsFinal) = getBlock1(puzzle, x, y)
        ((x in 3..5) and (y in 0..2)) -> blockNums = getBlock2(puzzle, x, y)
        ((x in 6..8) and (y in 0..2)) -> blockNums = getBlock3(puzzle, x, y)
        ((x in 0..2) and (y in 3..5)) -> blockNums = getBlock4(puzzle, x, y)
        ((x in 3..5) and (y in 3..5)) -> blockNums = getBlock5(puzzle, x, y)
        ((x in 6..8) and (y in 3..5)) -> blockNums = getBlock6(puzzle, x, y)
        ((x in 0..2) and (y in 6..8)) -> blockNums = getBlock7(puzzle, x, y)
        ((x in 3..5) and (y in 6..8)) -> blockNums = getBlock8(puzzle, x, y)
        ((x in 6..8) and (y in 6..8)) -> blockNums = getBlock9(puzzle, x, y)
    }
    return Pair(blockNums, blockNumsFinal)
}

I have found no answers in my google searches.

CodePudding user response:

This:

(blockNums, blockNumsFinal) =

looks like you're trying to use a destructuring declaration syntax for variables that are already declared. This is not allowed. You can only use it for declarations (hence the name).

Since your other function evidently already returns a pair, this should work, but I'm guessing because I'm not sure of the return types of your other functions.

You're also misusing the and infix function. It is intended for bitwise comparisons. When used with Boolean expressions, it prevents early-exiting of the total expression, which means if the first expression is false, it still redundantly calculates the second expression. Not what you want here. You should use && here.

fun block (puzzle: Array<IntArray>, x:Int, y:Int): Pair<MutableList<Int>, MutableSet<Int>> {
    return when {
        ((x in 0..2) && (y in 0..2)) -> getBlock1(puzzle, x, y)
        ((x in 3..5) && (y in 0..2)) -> getBlock2(puzzle, x, y)
        ((x in 6..8) && (y in 0..2)) -> getBlock3(puzzle, x, y)
        ((x in 0..2) && (y in 3..5)) -> getBlock4(puzzle, x, y)
        ((x in 3..5) && (y in 3..5)) -> getBlock5(puzzle, x, y)
        ((x in 6..8) && (y in 3..5)) -> getBlock6(puzzle, x, y)
        ((x in 0..2) && (y in 6..8)) -> getBlock7(puzzle, x, y)
        ((x in 3..5) && (y in 6..8)) -> getBlock8(puzzle, x, y)
        ((x in 6..8) && (y in 6..8)) -> getBlock9(puzzle, x, y)
        else -> Pair(mutableListOf(), mutableSetOf())
    }
}

About the syntax you were trying to use, that would be called "destructuring assignment" if it existed. There's a feature request on the Kotlin YouTrack page for it.

  • Related