Salvador Ferreira
Salvador Ferreira

Reputation: 1

Type mismatch: inferred type is unit but int is expected kotlin

Im trying to get multiple types of return (Int and Boolean) but i can only seem to get one at a time:

fun isValidPositions(p1: Int, p2: Int, pairs: List<Char>):Boolean{
    if(p1 !in 0..9 || p2 !in 0..9 ){
        println("Posições inválidas")
    }else if (p1 == p2){
        println("Posições inválidas")
    }else if(pairs.get(p1) != '_' || pairs.get(p2) != '_' )
        println("Posições inválidas")
    return true
}

then in my funcion main i have this:

 val first = readPosition("primeira")
        val second = readPosition("segunda")
        if ( isValidPositions(first, second, places) ) {
            places = places.play(first, second, pairs)

and it gives me the error: Type mismatch: inferred type is unit but int is expected i cant seem to understand how i can get 2 types of return when i need the true and an int im really new to coding since i just got into uni

Upvotes: 0

Views: 3020

Answers (1)

Joffrey
Joffrey

Reputation: 37660

Your problem is most likely that readPosition doesn't return anything (in Kotlin, that means returning Unit). This means that you're assigning Unit to your first and second variables instead of the Int value you're expecting.

Then when you reach isValidPositions, it wants an Int but you're passing Unit.

You should post your readPosition function to get more help on this.

Upvotes: 1

Related Questions