Does' if (foobar == ("this" || "that")) write to check if foobar is equal to "this" or "that" is supported in any major languages?

I have a faint memory of being able to write an if statement in some language:

if (foobar == ("this" || "that")) 

Instead:

 if (foobar == "this" || foobar == "that") 

The latter is more detailed and therefore less aesthetically pleasing to me.

Is the former convention supported in any major languages? Is there a reason why this is not widely supported?

+7
programming-languages if-statement
source share
6 answers

Icon programming language has this feature.

Using 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 0 or 1.

The basic idea is that each of these (1|2|3..) sequences creates a generator that will return each of the values ​​in turn until the value ends. When you use a generator in a logical simulation 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 possible combinations will be taken until success can be achieved. (Instead of returning false, the equality operator fails if the values ​​are not equal.)

+4
source share

It seems I remember that Pascal had an in operator, as in:

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

As for why it is not supported more widely, it hits me. It looks like a pretty nice piece of syntactic sugar.

+2
source share

The only place I remember using this syntax is COBOL, about 25 years ago.

I suspect that the reason it is not widely supported is because it leads to uncertainty that the compiler cannot solve. In your particular case, this is not a special problem, since "this" and "that" are strings for which the conditional OR operator does not make sense. But consider this fragment, in C language, where the conditional result is a Boolean value of 0 or 1:

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

At this point, the compiler cannot reliably determine what you want. You intend to do this:

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

or, did you intend to:

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

You can limit the types for which such syntax can be used, but then you accumulate exceptions instead of what ideally should be orthogonal syntax. This will confuse users and complicate the compiler.

It also forces expressions to be evaluated differently in conditional expressions than in assignment operators. This, of course, will complicate the compiler.

This can certainly be done for the job, but I think it will require a new syntax with overloaded characters and all for dubious benefits.

+2
source share

The author's language of interactive fiction (text adventure) Inform supports it with the help of the operator or (unlike the usual logical or which is written "||):

 if (qty==2 or 3) print "some"; else print "many"; 

Is there a reason why this is not widely supported?

This is a little special magic; he is not clear what he is doing in anything other than a simple expression of an example.

 if (n or 8==m+(1 or 2) or 7) // plausible if (qty<4 or 5) // huh? a= "cat" or "dog" // er 

"If the value in (sequence), which is available in Python and other modern scripting languages, has probably already occupied this niche.

+1
source share

Operator || is a binary operator that takes two operands and returns a single value. Because expressions are evaluated in most major languages, it would be impossible to use this syntax to check if a value matches one of a set of values. This is why it is not supported in most major languages.

The in syntax, which is published in the @ 1800 section, is supported by many languages. If it is not supported in your language, you can simply create a function called in() or in_array() that takes two arguments, a value and an array, and returns true if the array contains this value. (PHP, for example, has a built-in function in_array () .)

0
source share

In Python you can write like this

 if(foobar in ('this', 'that', 'and something else')) 

('this', 'that', 'and something else') is a type of list.

So, I think that 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)) 
0
source share

All Articles