Issue
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?
Solution
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:
truefalsenull
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.
Answered By - rzwitserloot
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.