You can use the + operator in OCL to add numeric values or join string values in derived attributes, constraints, and display expressions.
Syntax
leftOperand + rightOperandThe + operator is a binary operator: it combines two operands.
Add numbers
Use + when both operands are numeric values. In OCL, Number includes both Integer and Real values.
For example, if a Department has the numeric attributes PermanentStaff and ContractStaff, calculate the total staff count with:
self.PermanentStaff + self.ContractStaffThis expression returns the sum of the two values.
| Operand types | Result | Example |
|---|---|---|
| Integer + Integer | Integer | 3 + 2 returns 5
|
| Integer + Real | Real | 3 + 2.5 returns 5.5
|
| Real + Real | Real | 3.5 + 2.5 returns 6.0
|
When one numeric operand is a Real, the result is promoted to Real.
For the available arithmetic operators and advanced numeric functions, see Documentation:OCL Number Operators.
Concatenate strings
Use + when both operands are String values. The operator joins the values in the order written.
'MDriven' + ' Designer'The expression returns 'MDriven Designer'. The leading space in the second string becomes part of the result.
You can combine more than two strings by using + repeatedly:
'Permanent staff: ' + '12'This returns 'Permanent staff: 12'.
Choose operands of compatible types
The operands determine whether + performs numeric addition or string concatenation. Use two numeric operands for a calculation and two String operands for text. Do not rely on + to change a value from a number to text or from text to a number.
For example, keep a staff-total calculation numeric:
self.PermanentStaff + self.ContractStaffUse string operands when creating text:
'Total staff'Related operators
Use binary - to subtract values. A leading minus sign has a different role: unary - negates one numeric value before addition or subtraction is evaluated.
