E07540: is not compatible with a Boolean type
What went wrong
You're using a non-Boolean value in a context that requires Boolean. A condition is a Boolean, or a type that promotes to Boolean with '#^' (the promotion is single-step: a type whose '#^' gives Integer is not a condition). There is no truthiness - String, Integer and Date have no Boolean reading.
Worked examples
Compilable examples demonstrating this error and the correct fix:
- ek9 -q 937 (Why does EK9 reject non-Boolean values in if conditio...)
How to fix it
- Convert to Boolean using ?, ==, or method call
- For your own type with a natural true/false reading, define 'operator #^' returning a Boolean - every condition then accepts it and the compiler inserts the promotion
A String such as 'if label' is rejected the same way; write 'if label?' (is it set) or a comparison. The [EXAMPLE] section below shows a String as a ternary's control and the Boolean that replaces it.
Common causes
- Condition needs Boolean
- Logical operators need Boolean operands
See also
Examples
Every example is compiled by the EK9 compiler: the first fails exactly as captioned, the others fix it.
Fails with E07540: is not compatible with a Boolean type
#!ek9 defines module error.e07540.bad defines function choose() -> label as String <- rtn as String: String() //The control of a ternary must be a Boolean rtn: label <- "A" else "B"
Fixed: Use a Boolean control
#!ek9 defines module error.e07540.good defines function choose() -> useFirst as Boolean <- rtn as String: useFirst <- "A" else "B"
Questions and answers
These Q&A entries show E07540 and its fix: