A new kind of binary expression that tests whether the left-hand side equals one of the right-hand values, or a range of values.
It allows expressing intent in a very concise manner, on par with other modern languages.
Range of values
Example:
if (age is in 13 .. 20) // teenager
Grammar:
ShiftExpression is in ShiftExpression .. ShiftExpression
The first ShiftExpression
is the left-hand side, the other two are called lower and upper bound.
Semantics:
It evaluates the left-hand side only once.
Equivalent to
((auto ref lhs) => lowerBound <= lhs && lhs < upperBound)(lhsExpr)
List of values
Examples:
if (color is in (red, green, blue)) { }
alias basicColors = AliasSeq!(red, green, blue);
if (color is in basicColors) { }
if (color is in (basicColors, yellow)) { }
Grammar:
ShiftExpression is in ( Expression )
ShiftExpression is in ShiftExpression
Semantics:
If the right-hand side starts with a parenthesis, it’s a comma-separated list of options. Those options can be compile-time sequences which are expanded. Otherwise, the right-hand side must be a compile-time sequence.
Equivalent to:
(auto ref lhs) {
switch (lhs)
{
static foreach (enum option; AliasSeq!(options))
{
case option:
}
return true;
default:
return false;
}
}(lhsExpression)