Operations¶
Operations are special functions that can be applied to one or two expressions. Safe-DS has a fixed set of operations that cannot be extended. We distinguish between
- prefix operations (general form
<operator> <operand>
), and - infix operations (general form
<left operand> <operator> <right operand>
).
Operations on Numbers¶
Numbers can be negated using the unary -
operator:
- The integer negative three is
-3
. - The float negative three is
-3.0
.
The usual arithmetic operations are also supported for integers, floats and combinations of the two. Note that when either operand is a float, the whole expression is evaluated to a float.
- Addition:
0 + 5
(result is an integer) - Subtraction:
6 - 2.9
(result is a float) - Multiplication:
1.1 * 3
(result is a float) - Division:
1.0 / 4.2
(result is a float)
Finally, two numbers can be compared, which results in a boolean. The integer 3
for example is less than the integer 5
. Safe-DS offers operators to do such checks for order:
- Less than:
5 < 6
- Less than or equal:
1 <= 3
- Greater than or equal:
7 >= 7
- Greater than:
9 > 2
Logical Operations¶
To work with logic, Safe-DS has the two boolean literals false
and true
as well as operations to work with them:
- (Logical) negation (example
not a
): Output istrue
if and only if the operand is false:
not a |
false | true |
---|---|---|
true | false |
- Conjunction (example
a and b
): Output istrue
if and only if both operands aretrue
. Note that the second operand is always evaluated, even if the first operand isfalse
and, thus, already determines the result of the expression. The operator is not short-circuited:
a and b |
false | true |
---|---|---|
false | false | false |
true | false | true |
- Disjunction (example
a or b
): Output istrue
if and only if at least one operand istrue
. Note that the second operand is always evaluated, even if the first operand istrue
and, thus, already determines the result of the expression. The operator is not short-circuited:
a or b |
false | true |
---|---|---|
false | false | true |
true | true | true |
Equality Checks¶
There are two different types of equality in Safe-DS, identity and structural equality. Identity checks if two objects are one and the same, whereas structural equality checks if two objects have the same structure and content. Using a real world example, two phones of the same type would be structurally equal but not identical. Both types of equality checks return a boolean literal true
if the check was positive and false
if the check was negative. The syntax for these operations is as follows:
- Identity:
1 === 2
- Structural equality:
1 == 2
Safe-DS also has shorthand versions for negated equality checks which should be used instead of an explicit logical negation with the not
operator:
- Negated identity:
1 !== 2
- Negated structural equality:
1 != 2
Elvis Operator¶
The elvis operator ?:
(given its name because it resembles Elvis's haircut) is used to specify a default value that should be used instead if the left operand is null
. This operator is not short-circuited, so both operand are always evaluated. In the following example the whole expression evaluates to nullableExpression
if this value is not null
and to 42
if it is: