Thread overview
`is in` expression
4 days ago
Quirin Schroll
4 days ago
ltdk
4 days ago
monkyyy
2 days ago
Nick Treleaven
15 hours ago
Atila Neves
11 hours ago
Quirin Schroll
4 days ago

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)
4 days ago

On Friday, 5 September 2025 at 12:12:22 UTC, Quirin Schroll wrote:

>

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)

My stupid question: why not just 'in' ?
Julia has 'in' operator working with ranges

4 days ago

On Friday, 5 September 2025 at 12:12:22 UTC, Quirin Schroll wrote:

>

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.

[...]

thats like 5 new ideas or not generic in the slightest

2 days ago

On Friday, 5 September 2025 at 12:12:22 UTC, Quirin Schroll wrote:

>

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

I thought there might be something for this in Phobos (e.g. std.comparison), but I couldn't find it. E.g.:

age.between(13, 20)

>

List of values

Is it significantly better than:
https://dlang.org/phobos/std_algorithm_comparison.html#among

? among with runtime arguments also supports a predicate, so it's more flexible.

...

>

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.

Can the options be runtime values?

>

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)
15 hours ago

On Friday, 5 September 2025 at 12:12:22 UTC, Quirin Schroll wrote:

>

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.

[...]

The party line until now has been to not support this because of performance, namely the fact that it would usually be O(N).

11 hours ago

On Tuesday, 9 September 2025 at 11:07:42 UTC, Atila Neves wrote:

>

On Friday, 5 September 2025 at 12:12:22 UTC, Quirin Schroll wrote:

>

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.

[...]

The party line until now has been to not support this because of performance, namely the fact that it would usually be O(N).

It depends on how you count. Since the list is a compile-time sequence, one could argue it’s O(1). The compiler could optimize it.