Narrowing › boolean. The names in the examples are those of section 2.
A boolean alternative is the bare kind, true or false, or a single value: boolean = true, boolean = false. There is no interval and no order, so a boolean has the smallest sets of any kind, and the narrowing is mostly about which of the two values a branch leaves and whether null stays.
A boolean also has a role that the other kinds do not have: it can be the whole condition. if b then … is a fact about b: it is true in then, and anything else in else (false or null, since if takes the else branch for every value except true).
A boolean as the condition, and compared
| Condition | b when true | b when not true |
|---|---|---|
not(b) | boolean = false | boolean = true | Null |
b | boolean = true | boolean = false | Null |
b = true | boolean = true | boolean = false | Null |
b = false | boolean = false | boolean = true | Null |
b != true | boolean = false | Null | boolean = true |
b != false | boolean = true | Null | boolean = false |
b = b2 | boolean | Null | boolean | Null |
b < true | — | boolean | Null |
b > false | — | boolean | Null |
true < b | — | boolean | Null |
b <= true | — | boolean | Null |
b instance of boolean | boolean | Null |
balone is the same asb = true:trueinthen; inelsethe value isfalseornull.b != trueandb != falseare true fornullas well (null != trueistrue), soNullstays inthenand is removed fromelse, where the comparison is certainlyfalse.b = b2says little:b2can benull, and twonulls are equal.- Booleans have no order. Table 55 of the specification lists no boolean, and what is outside the domain of an operation is
null, sob < trueis a type mismatch:nullfor every value, and nevertrue. Thethenbranch cannot be taken, and theelsebranch is taken fortrueandfalseas much as fornull, sobis as it was there. (An earlier version left onlyNull, andif b < true then 1 else bwas typedNullwhere the run givestrue.) <=and>=follow the same ordered-kind restriction (D-196). The specification also gives the formulae1 < e2 or e1 = e2, which would maketrue <= truetrue. The supported interpretation follows the domain table: all four operators are typedNullwith atype_mismatchwarning, including when the other operand isAny(true <= v). Thethenbranch ofb <= trueis unreachable.- Booleans have no arithmetic either:
+ - * / **and a negation are not defined for one, sob + 1is a type mismatch, andb + vandv ** bareNullwith atype_mismatchwarning for everyv, a name of typeAnyincluded (D-203; see null). A condition that contains one,b + v > 1, is never true.
With and and or
b and b2 is true only when both are, so each is true in then. It is not true when either is not, so in else neither is narrowed. b or b2 is the other way round: when it is not true, both are false or null.
| Condition | b when true | b when not true |
|---|---|---|
b and b2 | boolean = true | boolean | Null |
b or b2 | boolean | Null | boolean = false | Null |
| Condition | b2 when true | b2 when not true |
|---|---|---|
b and b2 | boolean = true | boolean | Null |
A boolean of any other type
A name of type Any, used as a condition or compared with a boolean, is narrowed to the booleans:
| Condition | v when true | v when not true |
|---|---|---|
v | boolean = true | Any | Null |
v = true | boolean = true | Any except boolean = true | Null |
v = false | boolean = false | Any except boolean = false | Null |
Explicit comparisons keep exclusions such as Any except boolean = true | Null. A bare v used as a condition currently leaves Any | Null in the not-true branch; that is a precision limit of this fact, not an absence of complement types (Any).
What the condition's own type does
| Expression | Type | Warnings |
|---|---|---|
if b then b else true | boolean = true | — |
if not(b) then b else true | boolean | — |
if b < true then 1 else 2 | number = 2 | type_mismatch, decided_condition |
if b < true then 1 else b | boolean | Null | type_mismatch, decided_condition |
if b <= true then 1 else 2 | number = 2 | type_mismatch, decided_condition |
{ok: x > 5, r: if ok then x else 0}.r | number in 0, (5..max] | — |
not(b) is true where b is false, so in then b is boolean = false, and in else it is true or null (narrowing.md, the three outcomes). A boolean that was computed from a comparison (ok) is the value of the comparison, and a read of it as a condition says what the comparison says of x (narrowing.md).
Limits
There are none to speak of: the sets have two members. The cost of a long chain of and and or is that of the chain (limits).