Skip to content

Commit

Permalink
Merge pull request #14 from kennytm/no_fall_thru
Browse files Browse the repository at this point in the history
FAQ changes and Specify that implicit fall-through is disallowed.
  • Loading branch information
andralex committed Jul 5, 2011
2 parents c1b48a7 + ae093a0 commit 7620b55
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 24 deletions.
34 changes: 18 additions & 16 deletions faq.dd
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,10 @@ $(D_S $(TITLE),
$(ITEMR backend, How do I write my own D compiler for CPU X?)
$(ITEMR gui, Where can I get a GUI library for D?)
$(ITEMR ide, Where can I get an IDE for D?)
$(ITEMR q2, What about templates?)
$(ITEMR q3, Why emphasize implementation ease?)
$(ITEMR q4, Why is [expletive deleted] printf left in?)
$(ITEMR q5, Will D be open source?)
$(ITEMR q6, Why fall through on switch statements?)
$(ITEMR q6, Why $(I no) fall through on switch statements?)
$(ITEMR q7, Why should I use D instead of Java?)
$(ITEMR q7_2, Doesn't C++ support strings, etc. with STL?)
$(ITEMR q7_3, Can't garbage collection be done in C++ with an add-on library?)
Expand Down Expand Up @@ -165,20 +164,13 @@ $(ITEM gui, Where can I get a GUI library for D?)

$(P Since D can call C functions, any GUI library with a C interface is
accessible from D. Various D GUI libraries and ports can be found at
<a href="http://www.prowiki.org/wiki4d/wiki.cgi?AvailableGuiLibraries">AvailableGuiLibraries</a>.
$(LINK2 http://www.prowiki.org/wiki4d/wiki.cgi?GuiLibraries, the D wiki).
)

$(ITEM ide, Where can I get an IDE for D?)

$(P Try
$(LINK2 http://www.dsource.org/projects/elephant/, Elephant),
$(LINK2 http://www.dsource.org/projects/poseidon/, Poseidon),
or $(LINK2 http://www.dsource.org/projects/leds/, LEDS).
)

$(ITEM q2, What about templates?)

$(P D now supports advanced templates.
$(P A list of IDEs and editors that support D can be found on
$(LINK2 http://prowiki.org/wiki4d/wiki.cgi?EditorSupport, the D wiki).
)

$(ITEM q3, Why emphasize implementation ease?)
Expand Down Expand Up @@ -212,7 +204,7 @@ $(ITEM q4, Why is printf in D?)
$(P $(B printf) is not part of D, it is part of C's standard
runtime library which is accessible from D.
D's standard runtime library has $(B std.stdio.writefln),
which is as powerful as $(B printf) but much easier to use.
which is as powerful as $(B printf) but is much easier to use.
)


Expand All @@ -226,14 +218,21 @@ $(ITEM q5, Will D be open source?)
to create $(B gdc), a completely open source implementation of D.
)

$(ITEM q6, Why fall through on switch statements?)
$(ITEM q6, Why $(I no) fall through on switch statements?)

$(P Many people have asked for a requirement that there be a break between
cases in a switch statement, that C's behavior of silently falling through
is the cause of many bugs.
)

$(P The reason D doesn't change this is for the same reason that integral
$(P In D2, implicit fall through is disallowed. You have to add a
$(LINK2 statement.html#GotoStatement, goto case;) statement to explicitly
state the intention of falling through.
)

$(P There was further request that the $(B break) statement be made
implicit.
The reason D doesn't change this is for the same reason that integral
promotion rules and operator precedence rules were kept the same - to
make code that looks the same as in C operate the same. If it had subtly
different semantics, it will cause frustratingly subtle bugs.
Expand Down Expand Up @@ -764,7 +763,10 @@ $(ITEM reference-counting, Why doesn't D use reference counting for garbage coll

$(P That said, D may in the future optionally support some form of ref
counting, as rc is better for managing scarce resources like file
handles.
handles.
Furthermore, if ref counting is a must, Phobos has the
$(LINK2 phobos/std_typecons.html#RefCounted, std.typecons.RefCounted) type
which implements it as a library, similar to C++'s shared_ptr&lt;&gt;.
)

$(ITEM gc_1, Isn't garbage collection slow and non-deterministic?)
Expand Down
46 changes: 46 additions & 0 deletions statement.dd
Original file line number Diff line number Diff line change
Expand Up @@ -957,8 +957,11 @@ $(V2
Const or immutable variables must all have different names.
If they share a value, the first case statement with that value
gets control.
There must be exactly one default statement.
)
$(V1
There may not be two or more default statements.
)
)

$(P The $(GLINK ScopeStatementList) introduces a new scope.
Expand All @@ -980,6 +983,7 @@ $(V2
}
--------------

$(V1
$(P Case statements 'fall through' to subsequent
case values. A break statement will exit the switch $(I BlockStatement).
For example:
Expand All @@ -1002,6 +1006,48 @@ switch (i)

$(P will set $(CODE x) to $(CODE 4) if $(CODE i) is $(CODE 1).
)
)

$(V2
$(P A $(GLINK ScopeStatementList) must either be empty, or be ended with
a $(GLINK ContinueStatement), $(GLINK BreakStatement),
$(GLINK ReturnStatement), $(GLINK GotoStatement), $(GLINK ThrowStatement)
or assert(0) expression unless this is the last case. This is to
set apart with C's error-prone implicit fall-through behavior.
$(B goto case;) could be used for explicit fall-through:
)

--------------
int number;
string message;
switch (number)
{
default: // valid: ends with 'throw'
throw new Exception("unknown number");

case 3: // valid: ends with 'break' (break out of the 'switch' only)
message ~= "three ";
break;

case 4: // valid: ends with 'continue' (continue the enclosing loop)
message ~= "four ";
continue;

case 5: // valid: ends with 'goto' (explicit fall-through to next case.)
message ~= "five ";
goto case;

case 6: // $(B ERROR): implicit fall-through
message ~= "six ";

case 1: // valid: the body is empty
case 2: // valid: this is the last case in the switch statement.
message = "one or two";
}
--------------

$(P A break statement will exit the switch $(I BlockStatement).)
)

$(P $(LNAME2 string-switch, Strings can be used in switch expressions).
For example:
Expand Down
16 changes: 8 additions & 8 deletions template-mixin.dd
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,15 @@ $(V2 mixin) template duffs_device(alias id1, alias id2, alias s)
if (id1 < id2)
{
typeof(id1) n = (id2 - id1 + 7) / 8;
switch ((id2 - id1) % 8)
$(V2 final )switch ((id2 - id1) % 8)
{
case 0: do { s();
case 7: s();
case 6: s();
case 5: s();
case 4: s();
case 3: s();
case 2: s();
case 0: do { s(); $(V2 goto case;)
case 7: s(); $(V2 goto case;)
case 6: s(); $(V2 goto case;)
case 5: s(); $(V2 goto case;)
case 4: s(); $(V2 goto case;)
case 3: s(); $(V2 goto case;)
case 2: s(); $(V2 goto case;)
case 1: s();
} while (--n > 0);
}
Expand Down

0 comments on commit 7620b55

Please sign in to comment.