Skip to content

Commit ea2c81f

Browse files
ntreldlang-bot
authored andcommitted
Fix Bugzilla 24548 - [spec] Boolean condition conversion is not documented
Add subheading for boolean conditions. Also use subheading instead of panel for `if` condition variables.
1 parent cdec020 commit ea2c81f

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

spec/expression.dd

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2600,7 +2600,8 @@ $(GNAME AssertArguments):
26002600
$(GLINK AssignExpression) $(D ,) $(GLINK AssignExpression) $(D ,)$(OPT)
26012601
)
26022602

2603-
$(P The first $(I AssignExpression) is evaluated and converted to a boolean value.
2603+
$(P The first $(I AssignExpression) is evaluated and
2604+
$(DDSUBLINK spec/statement, boolean-conditions, converted to a boolean value).
26042605
If the value is not `true`, an $(I Assert Failure)
26052606
has occurred and the program enters an $(I Invalid State).
26062607
)

spec/statement.dd

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -251,20 +251,42 @@ $(GNAME ElseStatement):
251251
$(PSSCOPE)
252252
)
253253

254-
$(P If there is a declared *Identifier* variable, it is evaluated.
254+
$(P If there is a declared *Identifier* variable,
255+
$(RELATIVE_LINK2 condition-variables, it is evaluated).
255256
Otherwise, *Expression* is evaluated. The result is converted to a
256-
boolean, using $(DDSUBLINK spec/operatoroverloading, cast, `opCast!bool()`)
257-
if the method is defined.
257+
boolean.
258258
If the boolean is `true`, the $(I ThenStatement) is transferred
259259
to, otherwise the $(I ElseStatement) is transferred to.)
260260

261261
$(P The $(I ElseStatement) is associated with the innermost `if`
262262
statement which does not already have an associated $(I ElseStatement).)
263263

264-
$(PANEL
264+
$(H3 $(LNAME2 boolean-conditions, Boolean Conversion))
265+
266+
$(P The following type instances are supported in a condition:)
267+
268+
$(UL
269+
$(LI $(DDSUBLINK spec/type, FundamentalType, Fundamental types) are `true` when non-zero)
270+
$(LI Pointers are `true` when non-null)
271+
$(LI Reference types are `true` when non-null)
272+
$(LI A user-defined type with a valid $(DDSUBLINK spec/operatoroverloading, cast, `opCast!bool()`) method will be called for the result. Otherwise, any
273+
$(DDSUBLINK spec/struct, alias-this, `alias this`) member declaration
274+
will be used if its target type itself matches one of these rules.)
275+
$(LI An enum type is `true` if its base type matches one of these rules.)
276+
)
277+
$(P If none of these are valid, it is an error to use the value in a condition.)
278+
279+
$(NOTE A dynamic array can be used in a condition, which is `true` when non-null.
280+
However, using this should be avoided as it can be bug-prone.
281+
This may be disallowed in a future edition.)
282+
283+
$(H3 $(LNAME2 condition-variables, Condition Variables))
284+
285+
$(P
265286
When an $(I Identifier) form of *IfCondition* is used, a
266287
variable is declared with that name and initialized to the
267288
value of the *Expression*.
289+
)
268290

269291
* If $(D auto) $(I Identifier) is provided, the type of the variable
270292
is the same as *Expression*.
@@ -309,7 +331,8 @@ $(GNAME WhileStatement):
309331
$(P A $(I While Statement) implements a simple loop.)
310332

311333
$(P If the $(I IfCondition) is an *Expression*, it is evaluated and must have a type
312-
that can be converted to a boolean. If it's `true` the *ScopeStatement* is executed.
334+
that can be $(DDSUBLINK spec/statement, boolean-conditions, converted to a boolean).
335+
If it's `true` the *ScopeStatement* is executed.
313336
After the *ScopeStatement* is executed, the *Expression* is evaluated again, and
314337
if `true` the *ScopeStatement* is executed again. This continues until the *Expression*
315338
evaluates to `false`.)
@@ -351,7 +374,8 @@ $(GNAME DoStatement):
351374
$(P Do while statements implement simple loops.)
352375

353376
$(P *ScopeStatement* is executed. Then *Expression* is evaluated and must have a
354-
type that can be converted to a boolean. If it's `true` the loop is iterated
377+
type that can be $(DDSUBLINK spec/statement, boolean-conditions, converted to a boolean).
378+
If it's `true` the loop is iterated
355379
again. This continues until the *Expression* evaluates to `false`.)
356380

357381
---
@@ -387,8 +411,8 @@ $(GNAME Increment):
387411

388412
$(P $(I Initialize) is executed.
389413
$(I Test) is evaluated and must have a type that
390-
can be converted to a boolean. If *Test* is `true` the
391-
*ScopeStatement* is executed. After execution,
414+
can be $(DDSUBLINK spec/statement, boolean-conditions, converted to a boolean).
415+
If *Test* is `true` the *ScopeStatement* is executed. After execution,
392416
$(I Increment) is executed.
393417
Then $(I Test) is evaluated again, and if `true` the
394418
*ScopeStatement* is executed again. This continues until the

0 commit comments

Comments
 (0)