Operators

< Maps | Formatters > |

terp has a powerful expression syntax. It supports the usual assortment of mathematical, logical, comparison, and assignment operators for a variety of operand types. The following table provides a complete list of the known operators.

Operator | Name | Comments |
---|---|---|

+ | "+" | Arithmetic when used with numbers, additive when used with collections, concatenating when used with strings. |

- | "-" | Arithmetic when used with numbers, removing when used with collections. |

* | "*" | Multiplicative when used with numbers, repeat when used with other objects. |

/ | "/" | Divisive when used with numbers. |

% | "%" | Modulo when used with numbers. |

+= | "+=" | Requires lvalue as first operand. Arithmetic when used with numbers, additive when used with collections. |

-= | "-=" | Requires lvalue as first operand. Arithmetic when used with numbers, removing when used with collections. |

*= | "*=" | Requires lvalue as first operand. Multiplicative when used with numbers, repeat when used with other objects. |

/= | "/=" | Requires lvalue as first operand. Divisive when used with numbers. |

%= | "%=" | Requires lvalue as first operand. Modulo when used with numbers. |

= | "=" | Requires lvalue as first operand. Assignment. |

< | "<" | Less than comparison |

<= | "<=" | Less than or equals comparison. |

== | "==" | Equals comparison |

>= | ">=" | Greater than or equals comparison. |

> | ">" | Greater than comparison. |

!= | "!=" | Not equals comparison. |

! | "!" | Logical not operator. |

&& | "&&" | Logical and operator. |

|| | "||" | Logical or operator. |

~~ | "~~" | Pattern match operator. |

!~ | "!~" | Not pattern match operator. |

? : | "?:" | Conditional operator. |

.. | ".." | Range operator. |

++ | "++()" | Pre-increment operator. |

++ | "()++" | Post-increment operator. |

-- | "--()" | Pre-decrement operator. |

-- | "()--" | Post-decrement operator. |

- | "-()" | Negation (unary minus) operator. |

Operators are often overloaded by operand type. For example, terp takes totally different actions in the two snippets below because the operand types are different.

{1, 2, 3} + 4 1 + 4

In the first case, the integer `4`

is added to the collection already containing `1`

, `2`

, and `3`

, resulting in a collection containing 4 elements. In the second case, two integers are added for an integer result of `5`

. How is this overloading achieved and can you overload operators yourself?

The answer is that you can add operator overloads by registering operator executors with the terp registrars. This mechanism allows you to redefine the meaning of any of the above operators for your custom data types.