Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 469
core Avatar asked Mar 14 '09 04:03

core


4 Answers

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.)

like image 118
Eclipse Avatar answered Nov 15 '22 18:11

Eclipse


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.

like image 31
1800 INFORMATION Avatar answered Nov 15 '22 20:11

1800 INFORMATION


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.

like image 2
Jim Mischel Avatar answered Nov 15 '22 18:11

Jim Mischel


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.

like image 1
bobince Avatar answered Nov 15 '22 18:11

bobince