Programming languages ​​that contain shorthand equality logic to compare one variable with several

I would be interested to know if there are programming languages ​​that have “shorthand constructions of equality” (I just made this term, but not sure how to describe it).

So, instead of the usual:

if (X == 1 || X == 2)

Transcript verification type, for example:

if (X == 1 || 2)

I understand that for this type of design, there are many against. And that I can create functions to do something like that, but I would be wondering if there are languages ​​that allow you to do out of the box.


EDIT

Thanks to Michael for helping me clarify the situation, I like the way Python does it.

I will try and explain better, because looking at my question above, he doesn’t explain very well.

Instead of checking something in the collection or creating a collection in the background.

I am wondering if there are programming languages, when only a left variable is specified once, it will automatically create a left and right one for you.

So, I am writing this:

if (X == 1 || 2 || 3)

Will actually create

if (X == 1 || X == 2 || X == 3)

I understand that this pseudo-syntax is not very useful and that creating a collection is a great way to do this. But wondered if it existed anywhere.

+4
source share
2 answers

I know nothing about this syntax, but it is closely related to testing for established membership, which includes many languages, either as a language construct or in a library.

For example, consider this Python bit:

x = 1 if x in {1, 2}: #do something 

This works in Python 2.7+, where there is syntax for a set of literals. Earlier versions of Python will build the set as:

 x = 1 if x in set([1, 2]): #do something 

Besides the syntactic difference, the aforementioned set-based approach is also different in that all possible values ​​are evaluated with a high degree of probability. You could, for example, do this:

 x = 1 if x in {1, 2/0}: #do something 

You will get an error to divide by zero.

+2
source

Burn shell:

 case $x in one | 2 | thr33 | f0re) echo less than six;; esac # spot logic error 
+2
source

All Articles