Enum std.typecons.Flag
Defines a simple, self-documenting yes
/no
flag. This makes it easy for
APIs to define functions accepting flags without resorting to bool
, which is opaque in calls, and without needing to define an
enumerated type separately. Using
instead of Flag
!"Name"bool
makes the flag's meaning visible in calls. Each yes
/no
flag has
its own type, which makes confusions and mix-ups impossible.
The enum base type is
.
bool
Enum members
Name | Description |
---|---|
no
|
When creating a value of type , use for the negative option. When using a value
of type , compare it against or just false or 0 .
|
yes
|
When creating a value of type , use for the affirmative option. When using a
value of type , compare it against .
|
Example
Code calling getLine
(usually far away from its definition) can't be
understood without looking at the documentation, even by users familiar with
the API:
string getLine(bool keepTerminator) { ... if (keepTerminator) ... ... } ... auto line = getLine(false);
Assuming the reverse
meaning (i.e. "ignoreTerminator") and inserting the wrong
code compiles and runs with erroneous results.
After replacing the boolean parameter with an instantiation of
, code
calling Flag
getLine
can be easily read and understood even by people not
fluent with the API:
string getLine(Flag!"keepTerminator" keepTerminator) { ... if (keepTerminator) ... ... } ... auto line = getLine(Flag!"keepTerminator".yes);
Passing categorical data by means of unstructured bool
parameters is classified under "simple-data coupling" by Steve
McConnell in the Code Complete book, along with three other
kinds of coupling. The author argues citing several studies that
coupling has a negative effect on code quality.
offers a
simple structuring method for passing Flag
yes
/no
flags to APIs.
An alias can be used to reduce the verbosity of the flag's type:
alias KeepTerminator = Flag!"keepTerminator"; string getline(KeepTerminator keepTerminator) { ... if (keepTerminator) ... ... } ... // Code calling getLine can now refer to flag values using the shorter name: auto line = getLine(KeepTerminator.yes);
Authors
Andrei Alexandrescu, Bartosz Milewski, Don Clugston, Shin Fujishiro, Kenji Hara