Is writing `if (foobar == ("this" || "that"))` to check if foobar is equal to "this" or "that" supported in any major languages?

The Icon programming language has this feature.

Through the use of generators, you can do

if i = (0 | 1) then write("0 or 1")

which succeeds if i = 0 or i = 1. You can even do:

if (i | j | k) = (0 | 1) then write("0 or 1")

which succeeds if any of i, j, or k is equal to 0 or 1.

The basic idea behind this is that each of those (1|2|3..) sequences creates a generator that will return each of the values in turn until it runs out of values. When you use a generator in a boolean sitation like this, values will be requested from the generator until the comparison succeeds. When you combine two generators on either side of the comparison, all the possible combinations will be attempted until one succeeds. (Instead of return false, the equality operator "fails" if the values are not equal.)


I seem to recall Pascal had an in statement as in:

if x in (foo, bar) then
  ...

As to why it is not more widely supported, beats me. It seems like a pretty nice piece of syntactic sugar.


The only place I recall using that syntax is in COBOL, about 25 years ago.

I suspect the reason it's not widely supported is because it leads to ambiguities that the compiler can't resolve. In your specific case, it's not a particular problem because "this" and "that" are strings, for which the conditional OR operator makes no sense. But consider this snippet, in a language like C, where the result of a conditional is a Boolean value 0 or 1:

int a = 22;
int b = 99;
int rslt = SomeFunction();
if (rslt == (a || b))

At this point the compiler can't reliably determine what you want. Do you intend this:

if (rslt == a || rslt == b)

or, did you intend:

if ((rslt == 0 && a == 0 && b == 0) || (rslt == 1 && a == 1 && b == 1))

You could limit the types for which such syntax could be used, but then you're piling exceptions on top of what ideally should be an orthogonal syntax. That's going to confuse users and complicate the compiler.

It also forces expressions to be evaluated differently in conditionals than in assignment statements. That, too, would undoubtedly complicate the compiler.

It could certainly be made to work, but I think that it would require new syntax with overloaded symbols, and all for a questionable gain.