views:

187

answers:

6
+5  Q: 

If statements

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?

+2  A: 

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.

1800 INFORMATION
Same thing works in Python as well
David Zaslavsky
The syntax in Pascal used sets, which had to be of an integral type. No version of Pascal that I'm aware of supported sets of strings.
Jim Mischel
It seems like I recall Ada supporting this too, it probably didn't handle strings though.
Jeremy Bade
+3  A: 

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

Eclipse
+1  A: 

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.

Jim Mischel
A: 

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

The 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() or 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.)

yjerem
+1  A: 

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.

bobince
Chris Lutz
ahem. You're right, even Inform isn't that perverse!
bobince
A: 

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