Got a question that the wiki doesn't answer? Ask on the forum (preferred), or join us on IRC.



From Wiki
Jump to: navigation, search

As of version 3.3.1, CommandHelper supports traditional operators, in addition to still allowing the pure functional approach. (In fact, the operation functions are still used internally regardless). This allows you to write more readable code, by using more standard symbols instead of only functions. Using operators instead of functions is highly recommended for all new code, though the functional usage will continue to remain supported.

Consider the following perfectly valid code:

1   if(and(equals(@var, 3), lte(2, @var2))){
2      msg('Something');
3   }

This is fairly hard to read, and could quickly get even more complicated and harder to read the more conditions you add. Instead, you can use infix notation now, using standard C/Java operators. The same code as above, converted to the infix notation looks like:

1   if(@var == 3 && 2 <= @var2){
2      msg('Something');
3   }

Besides being less typing, it's much easier for a human to read, thanks to the operators. "@var equals 3 and 2 is less than or equal to @var2" as opposed to "and equals @var 3 lte 2 @var2". Using parenthesis is also supported, to force an order of operations:

1   if((@var == 3) && (2 <= @var2)){
2      msg('Something');
3   }

The following operators are supported, and their order of operations is from top to bottom. Note that all operators are simply converted to the functional notation, so if your code is incorrect, the errors you get will specify function names.

Type Symbol Function Conversion Notes
Postfix ++ -- postinc()/postdec() This is only considered postfix when it comes after an identifier: @i++
Unary  ! ++ -- not()/inc()/dec() These are unary operators, they only operate on one identifier
Exponential ** pow()
Multiplicative * / % multiply()/divide()/mod()
Additive + - . add()/subtract()/concat() If a minus or plus sign is used to denote the sign of a number, it is handled slightly differently, for instance, 2 + -1 does not use any subtraction
Relational < > <= >= lt()/gt()/lte()/gte()
Equality == != === !== equals()/nequals()/sequals()/snequals() There is no operational equivalent for equals_ic
Logical AND && and()
Logical OR || or()
Assignment = += -= *= /= .= assign() There is no single functional equivalent except for = per se, @var += 1 is equivalent to

assign(@var, add(@var, 1)), etc. += uses add(), -= uses subtract(), *= uses multiply(), /= uses divide(), and .= uses concat().

Array Sub-indices [ ] array_get()/array_set()/array_push() Using square braces allows for array accesses, and in combination with the = sign, setting sub-indices.

If the array set appears on the right hand side of an assignment, or in a general statement without an assign, it is an array_get operation. If it appears on the left hand side of an assignment, it is an array_set operation. The brackets apply to the element just preceeding, for instance with @var['index'], it is assumed that @var is an array or array like value. Empty braces, [], when on the left hand side works as an array push, and when on the right hand side, or in a general statement without an assign, it is an array clone operation (which ultimately still uses array_get). Sub-strings within strings may be pulled out using the bracket notation as well, and slices are supported. array(1, 2, 3)[1..2] returns an array with 2 elements in it, namely 2 and 3. 'string'[0] returns 's', and 'string'[0..1] returns 'st'.

Note the lack of bitwise operators, which are usually standard in other languages. These are not provided, because the operators are infrequently used, and may be used for other operations in the future. The functions themselves, bit_not(), bit_and(), and bit_or() still exist, so no functionality has been removed.

Also of note, auto-concatenation always takes lowest priority to all other operations.

Navigation menu