I have this function that returns a boolean value:
fun isSutableData(isAmount: Boolean, Value: String): Boolean {
val customValue = Value.replace(".", "").toLong()
val dataOverBase: Long
if (isAmount)
dataOverBase = (customValue * 100) / (baseAmount?.value ?: 1)
else
dataOverBase = customValue
return data in 1..dataOverBase
}
here how I use isSutableData function:
val isTiptooBig = isSutableData(isAmount, value)
and if statement:
if(isTiptooBig){
//some logic
}
on the if statement I get s error:
Type mismatch: inferred type is Boolean? but Boolean was expected
While I change if statement to this:
if(isTiptooBig == true){
//some logic
}
The error disappears.
Why do I get this error if isSutableData returns Boolean?
CodePudding user response:
inferred type is
Boolean?
This means that Kotlin believes that the isTiptooBig
expression is of type Boolean?
. Note the question mark. It's important. That means the value is one of 3 things:
true
false
null
And given that it's 3 things and not 2 things, if (that)
isn't allowed. However, if (that == true)
is (it would mean the if
block is not executed if that
is false
or null
; only if that
is true
does it execute.
Now, why does kotlin think that? I don't know - from what you pasted, it wouldn't think that. There must be more going on.
CodePudding user response:
inferred type is Boolean?
Boolean and Boolean? are different. 'Boolean?' means it will have an additional type i.e null other than true or false in this case.
Also, while you are returning i guess you are returning a range not boolean value. So, check the code once for that . It may solve the issue