Skip to content

Commit 4279d65

Browse files
MoonlightSentineldlang-bot
authored andcommitted
Replace throw statement with throw expressions (DIP 1034)
Updates the grammar rules and moves the documentation from `statement.dd` to `expression.dd`
1 parent bf07ff0 commit 4279d65

File tree

3 files changed

+40
-30
lines changed

3 files changed

+40
-30
lines changed

articles/lazy-evaluation.dd

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -249,12 +249,7 @@ if (!p)
249249
p.bar(); // now use p
250250
---
251251

252-
$(P Because throw is a statement, not an expression, expressions that
253-
need to do this need to be broken up into multiple statements,
254-
and extra variables are introduced.
255-
(For a thorough treatment of this issue, see Andrei Alexandrescu and
256-
Petru Marginean's paper
257-
$(LINK2 https://erdani.org/publications/cuj-06-2003.php.html, Enforcements)).
252+
$(P
258253
With lazy evaluation, this can all be encapsulated into a single
259254
function:
260255
)
@@ -312,4 +307,3 @@ Macros:
312307
SUBNAV=$(SUBNAV_ARTICLES)
313308

314309
NG_digitalmars_D = <a href="http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com$(AMP)group=digitalmars.D$(AMP)artnum=$0">D/$0</a>
315-

spec/expression.dd

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,7 @@ $(GNAME UnaryExpression):
939939
$(GLINK ComplementExpression)
940940
$(GLINK DeleteExpression)
941941
$(GLINK CastExpression)
942+
$(GLINK ThrowExpression)
942943
$(GLINK PowExpression)
943944
)
944945

@@ -1300,6 +1301,43 @@ $(H4 $(LNAME2 cast_void, Casting to `void`))
13001301
}
13011302
----
13021303

1304+
$(H2 $(LNAME2 throw_expression, Throw Expression))
1305+
1306+
$(GRAMMAR
1307+
$(GNAME ThrowExpression):
1308+
$(D throw) $(GLINK AssignExpression) $(D ;)
1309+
)
1310+
1311+
$(P
1312+
$(I AssignExpression) is evaluated and must yield a reference to a `Throwable`
1313+
or a class derived from `Throwable`. The reference is thrown as an exception,
1314+
interrupting the current control flow to continue at a suitable $(D catch) clause
1315+
of a $(GLINK2 statement, try-statement). This process will execute any applicable
1316+
$(LINK2 statement.html#ScopeGuardStatement, `scope (exit)` / `scope (failure)`)
1317+
passed since entering the corresponding `try` block.
1318+
)
1319+
1320+
---
1321+
throw new Exception("message");
1322+
---
1323+
1324+
$(P
1325+
A $(I ThrowExpression) may be nested in another expression:
1326+
)
1327+
---
1328+
void foo(int function() f) {}
1329+
1330+
void main() {
1331+
foo(() => throw new Exception());
1332+
}
1333+
---
1334+
1335+
1336+
$(BEST_PRACTICE Use $(DDSUBLINK spec/expression, assert_expressions, Assert Expressions)
1337+
rather than $(LINK2 $(ROOT_DIR)library/object#.Error, Error) to report program bugs
1338+
and abort the program.
1339+
)
1340+
13031341
$(H2 $(LNAME2 pow_expressions, Pow Expressions))
13041342

13051343
$(GRAMMAR

spec/statement.dd

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ $(GNAME NonEmptyStatementNoCaseNoDefault):
5757
$(GLINK SynchronizedStatement)
5858
$(GLINK TryStatement)
5959
$(GLINK ScopeGuardStatement)
60-
$(GLINK ThrowStatement)
6160
$(GLINK AsmStatement)
6261
$(GLINK MixinStatement)
6362
$(GLINK ForeachRangeStatement)
@@ -1174,7 +1173,7 @@ $(H3 $(LNAME2 no-implicit-fallthrough, No Implicit Fall-Through))
11741173

11751174
$(P A $(GLINK ScopeStatementList) must either be empty, or be ended with
11761175
a $(GLINK ContinueStatement), $(GLINK BreakStatement),
1177-
$(GLINK ReturnStatement), $(GLINK GotoStatement), $(GLINK ThrowStatement)
1176+
$(GLINK ReturnStatement), $(GLINK GotoStatement), $(GLINK2 expression, ThrowExpression)
11781177
or `assert(0)` expression unless this is the last case. This is to
11791178
set apart with C's error-prone implicit fall-through behavior.)
11801179

@@ -1747,27 +1746,6 @@ done
17471746
This restriction may be relaxed in future versions.
17481747
)
17491748

1750-
$(H2 $(LEGACY_LNAME2 ThrowStatement, throw-statement, Throw Statement))
1751-
1752-
$(P Throws an exception.)
1753-
1754-
$(GRAMMAR
1755-
$(GNAME ThrowStatement):
1756-
$(D throw) $(EXPRESSION) $(D ;)
1757-
)
1758-
1759-
$(P *Expression* is evaluated and must be a `Throwable` reference. The
1760-
`Throwable` reference is thrown as an exception.)
1761-
1762-
---
1763-
throw new Exception("message");
1764-
---
1765-
1766-
$(BEST_PRACTICE Use $(DDSUBLINK spec/expression, assert_expressions, Assert Expressions)
1767-
rather than $(LINK2 $(ROOT_DIR)library/object#.Error, Error) to report program bugs
1768-
and abort the program.
1769-
)
1770-
17711749
$(H2 $(LEGACY_LNAME2 ScopeGuardStatement, scope-guard-statement, Scope Guard Statement))
17721750

17731751
$(GRAMMAR

0 commit comments

Comments
 (0)