ObjectScript, we will develop the specification jointly: comparison operators

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

    The ObjectScript project is very young, only the first working and stable versions have recently appeared. Nevertheless, the question of the specification of some aspects of the language has not been completely resolved and is still under development. For example, comparison operators in different scripting languages ​​work differently, it depends on what types of data are involved in the comparison and what algorithm is used in a particular programming language.

    The question is quite sensitive, because depends on the result of the Boolean expression, not a lot is 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 figure out for sure what kind of behavior when comparing (these are the operators> =,>, <=, <, ==,! =) Of different data types in a scripting language is the most appropriate. Develop a collegial solution and consolidate the result in the ObjectScript specification.

    ObjectScript supports overloading comparison operators for objects, but I propose in the current question to focus on the operation of comparison operators only for primitive data types, 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 until the first invalid character, after which a number is returned (obtained at the current moment). 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 Compares


    JavaScript also converts arguments to numeric types if one of them is a number, and does this according to 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 encountered in the string, then NaN returns. As a result, if both arguments are numbers (NaN is also a number), then the comparison occurs in the usual mathematical way, but you need to keep in mind that any comparison with NaN always returns false. For example, with regular programming in PHP, NaN rarely pops up and it is almost never processed separately, so PHP strings will be converted to a valid number anyway, in case of an error, 0 will be returned, but in JS the situation is different.

    If the arguments are strings when comparing, then string comparison occurs.

    PHP does not have an undefined data type, but only null. When converting scripts from PHP to JS, we would always think about using jull 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);
    

    the console will display:

    '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 Compares


    Lua allows you to compare only numbers with numbers, strings with strings. Otherwise, it throws an error of the form: attempt to compare number with string. Even boolean doesn’t compare with a number, boolean doesn’t compare with boolean either. Instead of null, Lua uses nil. nil cannot be compared with a number; nil with nil is similar.

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

    How ObjectScript Compares


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

    As a result, the code for 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


    Let us summarize the results in one table:



    Question to experts


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

    Please speak in the comments, as I am sure that every vote will be on the score and perhaps it is your word that will be decisive in choosing the further path for the development of ObjectScript.

    Other relevant articles about ObjectScript:


    Also popular now: