I have a faint memory of being able to write an if statement in some language like this:
if (foobar == ("this" || "that"))
if (foobar == "this" || foobar == "that")
The latter is more verbose, and therefore less aesthetically appealing to me.
Is the former convention supported in any major languages? Is there a reason why this isn't widely supported?
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.
The interactive fiction (text adventure) authoring language Inform supports it using the ‘or’ operator (distinct from the usual logical-or which is written ‘||’):
if (qty==2 or 3) print "some"; else print "many";
Is there a reason why this isn't widely supported?
It's a bit special-case-magic; it's not clear what it does in anything other but the simple example expression.
if (n or 8==m+(1 or 2) or 7) // plausible if (qty<4 or 5) // huh? a= "cat" or "dog" // er
The ‘if value in (sequence)’ notation available in Python and other modern scripting languages has probably taken over this niche for good now.
|| operator is a binary operator which takes two operands and returns a single value. Because of the way expressions are evaluated in most major languages, it would be impossible to use that syntax to check if a value is equal to one of a set of values. That's why it isn't supported in most major languages.
in syntax that @1800 INFORMATION posted, however, is supported by many languages. If it isn't supported in your language, you could just create a function called
in_array() which takes two arguments, a value and an array, and returns
true if the array contains that value. (PHP, for example, has a built-in in_array() function.)
In Python you can write like this
if(foobar in ('this', 'that', 'and something else'))
('this', 'that', 'and something else') - it's a list type.
So, I think in any oop language you can create your own class:
MyClass(object): List values def in(value): return search_value_in_values(value)
and write something like this:
if(new MyClass('this', 'that', 'and something else').in(foobar))