soma.cell

E0039: int_as_bool

E0039 int_as_bool — a number, string or list where a condition needs a Bool.

A compile-time error of soma check, the checker of the Soma programming language (soma explain E0039).

The rule

Some builtins, such as regex_match, answer an Int (1 or 0), not a Bool, and a condition in Soma must be a Bool. soma check refuses such a call used directly as a require or if condition. Compare the result explicitly: require regex_match(email, pattern) == 1 else InvalidEmail. In 3.1.0 a plain Int variable used as a condition (if on_hand { … }) is reported as E0109 instead.

A program that triggers it

wrong.cell

cell Signup {
    face {
        signal register(email: String) -> String
    }
    memory {
        accounts: Map<String, String> [persistent]
    }
    on register(email: String) {
        require regex_match(email, "^[^@ ]+@[^@ ]+$") else InvalidEmail "not an email: {email}"
        accounts.set(email, "active")
        return email
    }
}

What soma check prints:

$ soma check wrong.cell
error: regex_match() answers an Int (1 or 0), not a Bool — compare it: `regex_match(…) == 1`
  --> wrong.cell:9:17
  |
9 |         require regex_match(email, "^[^@ ]+@[^@ ]+$") else InvalidEmail "not…
  |                 ^
  = code: E0039
✗ 1 error(s), 0 warning(s).

The fix field of soma check --json: compare it: `regex_match(…) == 1`

On line 9, require regex_match(email, "^[^@ ]+@[^@ ]+$") else InvalidEmail "not an email: {email}" becomes require regex_match(email, "^[^@ ]+@[^@ ]+$") == 1 else InvalidEmail "not an email: {email}".

wrong.cell → right.cell

--- wrong.cell
+++ right.cell
@@ -7,5 +7,5 @@
     }
     on register(email: String) {
-        require regex_match(email, "^[^@ ]+@[^@ ]+$") else InvalidEmail "not an email: {email}"
+        require regex_match(email, "^[^@ ]+@[^@ ]+$") == 1 else InvalidEmail "not an email: {email}"
         accounts.set(email, "active")
         return email

With the fix, soma check accepts the program:

$ soma check right.cell
✓ All checks passed.