next up previous contents
Next: Basic Classes Up: Expressions Previous: Isvoid   Contents

Arithmetic and Comparison Operations

Cool has four binary arithmetic operations: +, -, *, /. The syntax is

expr1 <op> expr2
To evaluate such an expression first expr1 is evaluated and then expr2. The result of the operation is the result of the expression.

The static types of the two sub-expressions must be Int. The static type of the expression is Int. Cool has only integer division.

Cool has three comparison operations: <, <=, =. For < and <= the rules are exactly the same as for the binary arithmetic operations, except that the result is a Bool. The comparisons =, < and <= are special cases. If either <expr1> or <expr2> has static type Int, Bool, or String, then the other must have the same static type. Any other types, including SELF_TYPE, may be freely compared. On non-basic objects, equality simply checks for pointer equality (i.e., whether the memory addresses of the objects are the same). Equality is defined for void.

In principle, there is nothing wrong with permitting equality tests between, for example, Bool and Int. However, such a test must always be false and almost certainly indicates some sort of programming error. The Cool type checking rules catch such errors at compile-time instead of waiting until runtime.

Finally, there is one arithmetic and one logical unary operator. The expression ~ <expr> is the integer complement of <expr>. The expression <expr> must have static type Int and the entire expression has static type Int. The expression not <expr> is the boolean complement of <expr>. The expression <expr> must have static type Bool and the entire expression has static type Bool.


next up previous contents
Next: Basic Classes Up: Expressions Previous: Isvoid   Contents