Skip to content

relational expression

Cameron Purdy edited this page Aug 5, 2021 · 2 revisions

Comparison for purposes of order uses a number of well-known expression forms, and adds a new operator:

a < b
a > b
a <= b
a >= b
a <=> b

The operators and their result types are as follows:

Name Op Result Type Also Known As
Less Than < Boolean
Greater Than > Boolean
Less Than/Equals <= Boolean Not Greater Than
Greater Than/Equals >= Boolean Not Less Than
Order Comparison <=> Ordered Spaceship operator

The rules for the traditional comparison operators (<, >, <=, >=) closely match the rules for the EqualityExpression above, including both input and output types:

  • There must be a bi-implicit type of the expressions a and b;

  • The type of the expressions a and b must both be compatible for purposes of equality;

  • That bi-implicit type must have an compare() function that matches a specific signature pattern:

    static <CompileType extends Orderable> Ordered compare(CompileType value1, CompileType value2);
    
  • The type of expression a can also be used to infer the type of the expression b;

  • The implicit type of the expression is Boolean.

The spaceship operator (<=>) is named for its similarity to the representation of spaceships in the old text-mode Star Trek game in BASIC. This operator is used to compare to values, to determine if the first is less than (<), equal to (=), or greater than (>) the second. The rules are identical to the comparison operators above, except that the implicit type of the expression is Ordered:

enum Ordered(String symbol) { Lesser("<"), Equal("="), Greater(">") }

The expression short-circuits if either expression a or b short-circuits.

The expression uses the default left-to-right definite assignment rules:

  • The VAS before a is the VAS before the expression.
  • The VAS before b is the VAS after a.
  • The VAS after the expression is the VAS after b.

The RelationalExpression groups to the left, so a < b < c is treated as (a < b) < c:

    RelationalExpression:
        RangeExpression
        RelationalExpression < RangeExpression
        RelationalExpression > RangeExpression
        RelationalExpression <= RangeExpression
        RelationalExpression >= RangeExpression
        RelationalExpression <=> RangeExpression