The Freya Programming Language

The membership checking operator

See also

The in binary operator is offered by Freya to perform membership tests against several data structures.

Membership tests

The in operator is a binary operator with same precedence as comparisons and the is operator:

expression in container

Freya supports this syntactic variant for negating the result of the test:

expression not in container

There are two different scenarios where in can be used:

In this case, in is translated like this:

ICollection[X](container).Contains(expression)

Please note that this includes the case where container is an array, since all arrays are assumed to implement the ICollection[X] generic interface.

In this case, there's no need for a typecast:

container.Contains(expression)

Membership tests against literal arrays

When the second operand is a literal array, the compiler can optimize the generated code. When the literal array has few enough elements, the in expression is translated as a concatenation of equality checks:

// Original test
expression in [item1, item2, item3]

// Generated code
var temp = expression;
temp = item1 or temp = item2 or temp = item3

When the literal array is long enough and when all of its items are constant expressions, the in test may be implemented with a switch IL operation code, according to the type of the left operand.

See also

The Freya Programming Language
Expressions
Nullable types
Type declarations
Interface types
Literal arrays