Skip to content

Commit 3c33899

Browse files
authored
[spec/statement] Improve *IfCondition* docs (#3705)
The type of *Expression* does not need to be convertible to boolean when the type of a declared identifier does instead. Mention `opCast!bool`. Avoid repetition. Tweak wording. Make example runnable.
1 parent a36729c commit 3c33899

File tree

1 file changed

+30
-25
lines changed

1 file changed

+30
-25
lines changed

spec/statement.dd

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -251,36 +251,38 @@ $(GNAME ElseStatement):
251251
$(PSSCOPE)
252252
)
253253

254-
$(P *Expression* is evaluated and must have a type that
255-
can be converted to a boolean. If it's `true` the
256-
$(I ThenStatement) is transferred to, else the $(I ElseStatement)
257-
is transferred to.)
258-
259-
$(P The $(I ElseStatement) is associated with the innermost if
254+
$(P If there is a declared *Identifier* variable, it is evaluated.
255+
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.
258+
If the boolean is `true`, the $(I ThenStatement) is transferred
259+
to, otherwise the $(I ElseStatement) is transferred to.)
260+
261+
$(P The $(I ElseStatement) is associated with the innermost `if`
260262
statement which does not already have an associated $(I ElseStatement).)
261263

262-
$(P If an $(D auto) $(I Identifier) is provided, it is declared and
263-
initialized
264-
to the value
265-
and type of the *Expression*. Its scope extends from when it is
266-
initialized to the end of the $(I ThenStatement).)
264+
$(PANEL
265+
When an $(I Identifier) form of *IfCondition* is used, a
266+
variable is declared with that name and initialized to the
267+
value of the *Expression*.
268+
269+
* If $(D auto) $(I Identifier) is provided, the type of the variable
270+
is the same as *Expression*.
267271

268-
$(P If a $(I TypeCtors) $(I Identifier) is provided, it is declared
269-
to be of the type specified by $(I TypeCtors)
270-
and is initialized with the value of the *Expression*.
271-
Its scope extends from when it is
272-
initialized to the end of the $(I ThenStatement).)
272+
* If $(I TypeCtors) $(I Identifier) is provided, the variable is
273+
declared to be the type of *Expression* but with $(I TypeCtors) applied.
273274

274-
$(P If a $(I Declarator) is provided, it is declared and
275-
initialized
276-
to the value
277-
of the *Expression*. Its scope extends from when it is
278-
initialized to the end of the $(I ThenStatement).)
275+
* If the $(I BasicType) form is provided, it declares the type of the
276+
variable as it would for a normal
277+
$(DDSUBLINK spec/declaration, variable-declarations, variable declaration).
279278

279+
$(P The scope of the variable is the *ThenStatement* only.)
280+
281+
$(SPEC_RUNNABLE_EXAMPLE_RUN
280282
---
281283
import std.regex;
282-
...
283-
if (auto m = std.regex.matchFirst("abcdef", "b(c)d"))
284+
285+
if (auto m = matchFirst("abcdef", "b(c)d"))
284286
{
285287
writefln("[%s]", m.pre); // prints [a]
286288
writefln("[%s]", m.post); // prints [ef]
@@ -289,10 +291,13 @@ if (auto m = std.regex.matchFirst("abcdef", "b(c)d"))
289291
}
290292
else
291293
{
292-
writeln(m.post); // Error: undefined identifier 'm'
294+
writeln("no match");
295+
//writeln(m.post); // Error: undefined identifier 'm'
293296
}
294-
writeln(m.pre); // Error: undefined identifier 'm'
297+
//writeln(m.pre); // Error: undefined identifier 'm'
295298
---
299+
)
300+
)
296301

297302
$(H2 $(LEGACY_LNAME2 WhileStatement, while-statement, While Statement))
298303

0 commit comments

Comments
 (0)