📜 ⬆️ ⬇️

ObjectScript, we develop the specification together: comparison operators

ObjectScript is a new embedded and very lightweight object-oriented open source programming language. ObjectScript extends the capabilities of languages ​​such as JavaScript, Lua and PHP. The syntax is mainly taken from JavaScript, the multiple assignment is from Lua, the work with properties is from PHP.

The ObjectScript project is quite young, only recently the first working and stable versions have appeared. Nevertheless, the question of the specification of certain aspects of the language is not completely resolved and is in the elaboration stage. For example, comparison operators in different scripting languages ​​work differently, it depends on which data types are involved in the comparison and which algorithm is used in a particular programming language.

The question is quite delicate, because depends on the result of a boolean expression, not a lot is not enough, a piece of code will be executed or not. The Russian proverb “one head is good, but much better” tells me that it is better to turn to the programming community and find out for sure what kind of behavior when comparing (these are operators> =,>, <=, <, ==,! =) Different data types in a scripting language is most appropriate. Develop a collective solution and fix the result in the ObjectScript specification.
')
ObjectScript supports overloading of comparison operators for objects, but in the current question I propose to stop the operation of comparison operators for primitive data types only, these are null, boolean, number and string.

How PHP works when comparing


PHP converts arguments to numeric types, if one of the arguments is a number. null is converted to 0, true to 1, false to 0, string is converted to a number up to the first non-valid character, and then a number is returned (the result so far). As a result, if both arguments are numbers, a numerical comparison occurs, if the strings are string.

Run the following PHP script:

echo "'a' > 'b' \t--> "; var_dump('a' > 'b'); echo "'a' < 'b' \t--> "; var_dump('a' < 'b'); echo "'a' > 1 \t--> "; var_dump('a' > 1); echo "'a' < 1 \t--> "; var_dump('a' < 1); echo "'1' > 1 \t--> "; var_dump('1' > 1); echo "'1' < 1 \t--> "; var_dump('1' < 1); echo "'+2.9' > 1 \t--> "; var_dump('+2.9' > 1); echo "'+2.9' < 1 \t--> "; var_dump('+2.9' < 1); echo "'2.9' > 1 \t--> "; var_dump('2.9' > 1); echo "'2.9' < 1 \t--> "; var_dump('2.9' < 1); echo "true > 1 \t--> "; var_dump(true > 1); echo "false < 1 \t--> "; var_dump(false < 1); echo "null > 1 \t--> "; var_dump(null > 1); echo "null < 1 \t--> "; var_dump(null < 1); 

he will output:

 'a' > 'b' --> bool(false) 'a' < 'b' --> bool(true) 'a' > 1 --> bool(false) 'a' < 1 --> bool(true) '1' > 1 --> bool(false) '1' < 1 --> bool(false) '+2.9' > 1 --> bool(true) '+2.9' < 1 --> bool(false) '2.9' > 1 --> bool(true) '2.9' < 1 --> bool(false) true > 1 --> bool(false) false < 1 --> bool(true) null > 1 --> bool(false) null < 1 --> bool(true) 

How javascript works when comparing


JavaScript also converts arguments to numeric types, if one of them is a number, and does so by the following rules: null to 0, undefined to NaN, true to 1, false to 0, string is converted to a number, if an invalid character is found in the string, then comes back NaN. As a result, if both arguments are numbers (NaN is also a number), then the comparison takes place in the usual mathematical way, but it must be kept in mind that any comparison with NaN always returns false. For example, with regular PHP programming, NaN rarely pops up and it is almost never processed separately, PHP lines will be converted to a valid number anyway, in case of an error, 0 is returned, but in JS the situation is different.

If the arguments are a string, the string comparison occurs.

PHP has no data type undefined, only null. When converting scripts from PHP to JS, we would always think that using JS instead of PHP-null, or JS-th null, or JS-th undefined. Therefore, we are obliged to include undefined in the JS test and check how it works in comparison with the number.

Run the following JS code in the browser:

 console.log("'a' > 'b' \t--> ", 'a' > 'b'); console.log("'a' < 'b' \t--> ", 'a' < 'b'); console.log("'a' > 1 \t--> ", 'a' > 1); console.log("'a' < 1 \t--> ", 'a' < 1); console.log("'1' > 1 \t--> ", '1' > 1); console.log("'1' < 1 \t--> ", '1' < 1); console.log("'+2.9' > 1 \t--> ", '+2.9' > 1); console.log("'+2.9' < 1 \t--> ", '+2.9' < 1); console.log("'2.9' > 1 \t--> ", '2.9' > 1); console.log("'2.9' < 1 \t--> ", '2.9' < 1); console.log("true > 1 \t--> ", true > 1); console.log("false < 1 \t--> ", false < 1); console.log("null > 1 \t--> ", null > 1); console.log("null < 1 \t--> ", null < 1); console.log("undefined > 1 \t--> ", undefined > 1); console.log("undefined < 1 \t--> ", undefined < 1); 

in the console will be displayed:

 'a' > 'b' --> false 'a' < 'b' --> true 'a' > 1 --> false 'a' < 1 --> false '1' > 1 --> false '1' < 1 --> false '+2.9' > 1 --> true '+2.9' < 1 --> false '2.9' > 1 --> true '2.9' < 1 --> false true > 1 --> false false < 1 --> true null > 1 --> false null < 1 --> true undefined > 1 --> false undefined < 1 --> false 

How Lua Works When Comparing


Lua allows you to compare only numbers with numbers, strings with strings. Otherwise, throws an error like: attempt to compare number with string. Even boolean cannot be compared with the number, boolean cannot be compared with boolean either. Instead of null, Lua uses nil. nil compare with the number does not work, nil c nil similarly.

In general, Lua testing as such failed on this issue.

How ObjectScript works when comparing


If one of the comparison arguments is a string, then a string comparison occurs. Otherwise (if the arguments are primitive types), then they are converted into numbers and a mathematical comparison takes place. null is converted to 0, true to 1, false to 0, the string is completely converted to a number, if there are no valid characters in the string, then 0 is returned.

As a result, the code on the OS:

 print("'a' > 'b' \t--> ", 'a' > 'b') print("'a' < 'b' \t--> ", 'a' < 'b') print("'a' > 1 \t--> ", 'a' > 1) print("'a' < 1 \t--> ", 'a' < 1) print("'1' > 1 \t--> ", '1' > 1) print("'1' < 1 \t--> ", '1' < 1) print("'+2.9' > 1 \t--> ", '+2.9' > 1) print("'+2.9' < 1 \t--> ", '+2.9' < 1) print("'2.9' > 1 \t--> ", '2.9' > 1) print("'2.9' < 1 \t--> ", '2.9' < 1) print("true > 1 \t--> ", true > 1) print("false < 1 \t--> ", false < 1) print("null > 1 \t--> ", null > 1) print("null < 1 \t--> ", null < 1) 

will output the following:

 'a' > 'b' --> false 'a' < 'b' --> true 'a' > 1 --> true 'a' < 1 --> false '1' > 1 --> false '1' < 1 --> false '+2.9' > 1 --> false '+2.9' < 1 --> true '2.9' > 1 --> true '2.9' < 1 --> false true > 1 --> false false < 1 --> true null > 1 --> false null < 1 --> true 

Total


We will summarize the results in one table:



Question to experts


So, what kind of comparison behavior should be considered as a reference for a scripting programming language and fixed in the ObjectScript specification?

Speak please in the comments, tk. I am sure that every vote will be counted and perhaps your word will be decisive in choosing the future path of development of ObjectScript.

Other relevant ObjectScript articles:

Source: https://habr.com/ru/post/154277/


All Articles