Operators
Supported operators
Arithmetic operators
| Operator | Type | Description |
|---|---|---|
+ | Binary | Sum |
- | Binary | Subtraction |
- | Unary | Negation |
/ | Binary | Division |
* | Binary | Multiplication |
% | Binary | Modulo |
^ | Binary | Power |
Boolean operators
| Operator | Type | Description |
|---|---|---|
&& | Binary | Logic And |
| || | Binary | Logic Or |
! | Unary | Logic Not |
Comparison operators
| Operator | Type | Description |
|---|---|---|
== | Binary | Equality |
!= | Binary | Inequalit |
> | Binary | Greater |
>= | Binary | Greater or equal |
< | Binary | Less |
<= | Binary | Less or equal |
Concatenation operator
| Operator | Type | Description |
|---|---|---|
.. | Binary | Concatenation |
This operator is an alternative sum operator that can be used
to join for example String or Array values.
Bless operator
| Operator | Type | Description |
|---|---|---|
@ | Binary | Change prototype |
This operator can be used to change the prototype of an
Object.
Iterator operators
| Operator | Type | Description |
|---|---|---|
> | Unary | Get value's iterator |
* | Unary | Advance iterator |
Bitwise operators
| Operator | Type | Description |
|---|---|---|
& | Binary | And |
| | Binary | Or |
>> | Binary | Right shift |
<< | Binary | Left shift |
^^ | Binary | Xor |
^ | Unary | Not |
Size operator
| Operator | Type | Description |
|---|---|---|
# | Unary | Get the the size of a value |
This operator can be used to retrieve the size of some value,
see for example String or Array.
Binary operators precedence
Operators with higher precedence are processed as operands of operators with lower precedence. Lower numbers mean higher precedence.
| Precedence | Operators |
|---|---|
| 1 | . |
| 2 | % / * |
| 3 | + - ^^ |
| 4 | >> << |
| 5 | > < >= <= @ |
| 6 | == != .. |
| 7 | & |
| 8 | ^ |
| 9 | | |
| 10 | && |
| 11 | || |
| 12 | ?? |
As an example:
Would be processed as:
Operators overloading
Melon supports operator overloading, this means that you can add your custom logic to be executed when an operator is used on Object operands.
To overload an operator, Symbol keys are used. Each operator has its own key which is stored inside the object core module.
To perform the overload you can simply store a Function or method with the custom operator's logic at its corresponding Symbol key:
As can been seen in the example above, you can combine Object prototypes with operator overloading to overload one or more operators for all objects which extend a given prototype. This is somewhat akin to overloading operators for all instances of a class in traditional OOP.
Custom operator's function
When writing a custom operator's function you should keep in mind which arguments you are going to get, as well as the value you are going to return.
For unary operators there's only a single value, the operand. For binary operators you are going to get two argument with both operands.
The result of the operator applied on the operand(s) must be returned from your function.
This is true for most operators except for example the setPropertyOperator or the setIndexOperator.
All custom operator's functions can return the value false to abort the custom operator and simply try to fallback to the default behaviour.
This is useful for example in conjunction with the *PropertyOperator or *IndexOperator:
You can also overload the setPropertyOperator and setIndexOperator to create objects which can not be modified from the outside:
note
Considering the the first argument in all operators is the left hand side of a binary operand or the unary operand, you can use methods to define a custom operator's behaviour and just use the this argument to refer to the first operand.
Supported custom operators
| Symbol | Operator |
|---|---|
object.symbols.sumOperator | + |
object.symbols.subOperator | - |
object.symbols.mulOperator | * |
object.symbols.divOperator | / |
object.symbols.concatOperator | .. |
object.symbols.compareOperator | This can be used to implement ==, <, <=, > and >=. The operator's Function should return an integer with the result of the left_hand_value - right_hand_value expression |
object.symbols.getIndexOperator | obj[i] where i is an integer |
object.symbols.setIndexOperator | obj[i] = <expr> where i is an integer |
object.symbols.negOperator | Unary - |
object.symbols.sizeOperator | # |
object.symbols.powOperator | ^ |
object.symbols.callOperator | obj(), can be used to implement functors |
object.symbols.hashingFunction | Can be used to supply custom hashing for an object, to be used as key of another Object. The operator's Function should return an integer representing the hash. |
object.symbols.iterator | Unary > |
object.symbols.nextFunction | Unary * |
object.symbols.getPropertyOperator | obj.k |
object.symbols.setPropertyOperator | obj.k |