It is right in the challenge. It could exist, not with this syntax that is weird and maybe creates ambiguity, but another could.
Math is like that
In fact mathematics itself has a chain of operators. But a human can parse the expression with more context than a compiler.
Simple chaining can make it difficult to compiler creation and decrease their performance and even prevent the resolution of certain cases.
In fact the chaining could generate misinterpretation on the intention of the programmer. Nothing serious, nothing that doesn’t happen in other cases of language syntax, but it’s something they’d rather avoid. You probably would have to have certain restrictions of what you can use, for example not being able to mix operators.
Examples of what can be adopted
The most interesting cases would be:
x > 0 && x < 10
which could be written as
x between 0, 10
and also
x == 1 || x == 2
which would be simplified to
x in (1, 2)
The names could be replaced by symbols.
In fact there are less popular languages that adopt this type of syntax.
I find the design pattern (yes, it’s a Pattern design) so common it could be worth.
You can think of other syntaxes, even more generic. For example, it could have a symbol, for example $
, where you use to indicate the name of the last variable used, it would already simplify. I’m not saying that this is good and there are no problems, just an idea. Many people will find it weird and less readable, essentially to save typing, which is silly to worry about.
Python lets do 0 < x < 10
. In some languages this would be interpreted as (0 < x) < 10
and it would work, but it would give an unexpected result. The operation inside the parentheses would generate a boolean which would then be used to compare with 10. It has languages that can identify that a Boolean cannot mix with a number and adopt the behavior of chained operator, but it is an exception, it is complicated to deal with this in the compiler. And there are situations that the compiler would lose. There are worse situations, including that Python generates something possibly unexpected.
Characteristics that can generate bugs are usually avoided by languages.
Performance
One of the advantages of the operator in
is that it is solved in compilation and does not generate running cost, and can even be generated a different algorithm depending on the amount of operands or other factors, unlike the one exposed by Isac that gives a solution that can be good in certain scenarios.
It’s not all flowers
But I see a problem that may be the reason for the more modern languages. I’m not going to talk about the older ones because they were rudimentary.
Everything you do in one language has consequences elsewhere. The more it complicates a point, it may be exponentially complicating other parts. Unless the functionality is completely orthogonal, which is rare, you’ll have to think of solutions to other points.
Overload of operators
If the operator in
is an operator like any other he should have the ability to be overloaded like the others, right? Then the execution could not be optimized properly and would fall into the same commitment to use a function that does this.
You may think that in languages that do not have operator overload can do without problems. But if you do so, it practically prevents language from being overloaded in the future. Many features when placed in the language prevent others, so it is good to avoid certain things.
Of course you could make an exception and not have overload for certain operators. This already exists, but it can get weird in this case because the simple comparison has overload and the multiple has no?
Even if you have overload I think normal or specific optimizations may still make it worth using.
The same goes for the chained greatness comparator, the first example I showed.
Other problems
Certainly in a quick analysis I haven’t looked at all the problems that might have in this functionality. It’s possible they thought of others I didn’t realize.
Of course I think for everything has solution. The question is whether it is good enough.
Psychological factors
People are used to a syntax, when it introduces a new one there is always some difficulty of people to accept. Any language that does this will have the pecha to be weird, even if it is just a baseless perception. No one wants that in their language.
There are already controversies about the use of ==
which is easy to confuse with =
.
Endnotes
Boolean expressions are independent and can be used anywhere a boolean value is expected. It’s common for people to think that comparisons can only be used in one if
or while
.
Some answers talk about Boolean algebra. Although the subject has to do with it, the question of the syntax of the operator has nothing to do with the algebra itself. No matter how the syntax generates a boolean result is what matters, the fact that it has this or that syntax matters nothing from the Boolean point of view and is not the reason for not having in the languages. It also has nothing to do with the generated Assembly code. Syntax is something independent.
C# 9
Now you can do something close to that, maybe even better:
name is "Girl" or "girl"
Behold working in the ideone (hoping to update version). And in the .NET Fiddle. Also put on the Github for future reference.
The other answer says this is impossible, so C# is miraculous.
Another novelty that should come in future versions is the ternary operator of comparison. Do not confuse it with the conditional ternary that already exists. That’s why I always said not to call the parole ternary. So you can do like Python.
The reason is quite simple, it has been defined that each comparison has to have 2 operands. What you are trying to do is create an exception to the rule
– Isac
Related: It’s a bad practice to make this comparison?
– vinibrsl