E07530: only compatible with Boolean type

Category: E07xxx Full resolution · Reported in phase: FULL_RESOLUTION · Classification: ONLY_COMPATIBLE_WITH_BOOLEAN · All error codes

What went wrong

An expression is used in a Boolean context (like condition) but its type isn't compatible with Boolean. A condition is a Boolean, or a type that promotes to Boolean with '#^' - the same rule for require, assert, if, while and a ternary.

Worked examples

Compilable examples demonstrating this error and the correct fix:

  • ek9 -q 156 (How do I assert conditions in a test in EK9?)
  • ek9 -q 305 (What is the difference between require, assert, and throw?)

How to fix it

  1. Use Boolean expression or conversion
  2. Use ? to check if set
  3. Use == for comparison
  4. For your own type with a natural true/false reading, define a '#^' (promotion) returning a Boolean

Common causes

  • Using non-Boolean type as condition
  • Missing conversion to Boolean

See also

  • E07530: Boolean context needs Boolean (this error)
  • E07540: non-Boolean value in Boolean context

Examples

Every example is compiled by the EK9 compiler: the first fails exactly as captioned, the others fix it.

Fails with E07530: only compatible with Boolean type

#!ek9
defines module error.e07530.bad

  defines function

    checkCount()
      -> count as Integer

      //require needs a Boolean
      require count

Fixed: Require a Boolean, such as a set-check

#!ek9
defines module error.e07530.good

  defines function

    checkCount()
      -> count as Integer

      //'?' gives a Boolean: is count set?
      require count?

Questions and answers

These Q&A entries show E07530 and its fix: