Math Expression Abbreviated Form

I’m currently testing a syntax of math expressions in an abbreviated form:

// XieXie
bool b1 := x < y < z
bool b2 := 0 < num < 10
bool b3 := x > y + 3 >= z/2 > 0

// C++
bool b1 = x < y && y < z;
bool b2 = 0 < num && num < 10;
bool b3 = x > y + 3 && y + 3 >= z/2 && z/2 > 0;

In C++ those expressions are allowed as well but usually end up in an unexpected result, e.g. consider the following code sample:

bool b = x < y < z;

A C++ compiler will generate code that evaluates (x < y), which results in true or false.
Then this boolean value will be compared with (… < z). And this is guaranteed not what you've expected.

In Java, those expressions are not allowed, since '<', '<=', '>‘ and ‘>=’ comparisions between boolean and integer are not defined.
In XieXie those comparisions are actually not allowed as well, but in the above examples you can make use of the math expressions in the abbreviated form.

This is actually not a big deal but I currently have some trouble with the parser for distinguishing the syntax of templates and the relation binary operator.
The fact that templates use a similar syntax as in C++ you should not mix those comparisions, i.e. don’t write expressions like in the following example:

// False example
bool b := x < y > z

The parser will expect that ‘x’ is a template and the argument is ‘y’. What you can do in such situations is to split this up:

// XieXie
bool b := x < y < z && (u > v > w)

// C++
bool b = x < y && y < z && (u > v && v > w);

This feature can be useful for instance in GUI programming:

class Button {
    bool MouseOver(int mx, int my) readonly {
        ret x <= mx <= x + w &&
            y <= my <= y + h
    }
private:
    [[set,get]] int x, y, w, h // <-- X, Y, Width, Height
}

Leave a Reply