I have a faint memory of being able to write an if statement in some language like this:
if (foobar == ("this" || "that"))
Instead of:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With