Comparison

From MiniScript Wiki
Jump to navigation Jump to search

Comparison operators: == ,  != , > , < , >= , <= .

Equality

The operator == tests for equality; the operator != is the negation of equality. These operators always result in true or false (or in some cases of incompatible types - null). Both can be applied to any values, not necessarily of the same type. If the values have different types, they're considered not equal. Otherwise, they're compared according to their types:

  • null is equal to itself
  • numbers and strings are compared in the usual way. (Unlike in some other languages, 0 == "0" is false.)
  • lists are equal if they contain equal elements
  • maps are equal if they contain equal key/value pairs
  • funcRefs are equal if they reference the same function
  • The special value NaN is not equal to anything, even to itself

To compare lists and maps by reference, use refEquals function.

Relational operators

Comparisons > , < , >= , <= are defined for numbers (arithmetic comparison) and strings (alphabetic comparison). These cases result in true or false.

For other data types or for operands of different data types they return null.

If the operands are a number and a null, they substitute null with 0 (resulting in true or false).

If the operands are a string and a null, it considers the string being always greater than null (resulting in true or false).

All six operators support chaining.

Examples

print null == null      // prints 1
print 0 != "0"          // prints 1
print 13 < 42 <= 420    // prints 1
print 2 < 15            // prints 1
print "2" < "15"        // prints 0, alphabetic comparison
print 0/0 == 0/0        // prints 0, NaN != NaN
print [1, 2, 3] == [1, 2, 3]    // prints 1
print 0 != null and 0 >= null and 0 <= null  // prints 1 :)