Authors: Dan Vesely <dan@suse.cz>, Michal Svec <msvec@suse.cz>.
YCP knows the following operators:
comparison operators,
boolean operators,
bit operators,
math operators,
triple operator.
Operators precedence. - Important!
comparison operators
Note: Using brackets makes code cleaner, but is not necessary (according to operators precedence).
Note: With the introduction of the
index operator ( a = mapvar["key"]:default ),
the sequence "]:" became a lexical token usable only for indexing,
so watch out when using the triple operator with lists and maps. Use
parentheses or white space.
Note: The marked operators'
precedence is different from those of the C language. This is
a bug (#21251, #21260) which will be fixed in the future so
you should always use parentheses in expressions involving
<<, >>, & and |.
These are binary operators for comparison of two values. The result is always boolean.
boolean operators
Operator Datatype Description == almost all True if operands are equal, otherwise false. < almost all True if left operand is smaller than the right one, otherwise false. > almost all True if left operand is greater than the right one, otherwise false. <= almost all True if left operand is smaller or equal to the right one, otherwise false. >= almost all True if left operand is greater or equal to the right one, otherwise false. != almost all True if operands are not equal, otherwise false.
These are logical operators, that works with boolean datatype, two are binary one is unary. The result is always boolean.
bit operators
Operator Datatype Description && boolean True if both operands are true, otherwise false (logical and). || boolean True if at least one of the operands is true, otherwise false (logical or). ! boolean True if the operand if false, otherwise false (logical not).
These are bit operators that works with integer, two are binary one is unary. The result is always integer.
math operators
Operator Datatype Description & integer Bits of the result number are product of the bits of the operands (bit and). | integer Bits of the result number are count of the bits of the operands (bit or). ~ integer Bits of the result number are reverted bits of operand (bit not). << integer Bits of the result number are left shifted bits of the operands (bit shift left). >> integer Bits of the result number are right shifted bits of the operands (bit shift right).
There math operators works with numeric data types (integer and float) and also with string. All are binary (except unary minus).
triple operator
Operator Datatype Description + integer, float, string The result is sum of the numbers or concatenation of the strings. - integer, float The result is difference of the numbers. * integer, float The result is product of the numbers. / integer, float The result is quotient of the numbers (number class is preserved, thus quotient of integers produce integer, etc). % integer The result is modulo. unary - integer, float The result is negative number.
This is the operator known from C language ( condition ? expression : expression). The first operand is expression that can evaluate to boolean, types of second and third operands are code dependent. The result of the triple operator expression is the second operand in the case the first operand (condition) evaluates to true, the third one otherwise.
operators precedence
Code Result Comment (3 > 2) ? true : false true The expression (3 > 2) evaluates to true, the result is true contains ([1, 2, 3], 5) ? "yes" : "no" "no" The expression contains ([1, 2, 3], 5) evaluates to false, the result is "no" (size ([]) > 0) ? 1 : -1 -1 The expression size ([]) > 0 evaluates to false, the result is -1
The table of operators precedence (from lowest to highest).
Direction Operators right = left ?: left || left && left == != left < <= > >= left + - left * / % left << >> left | left & prefix ! ~ -