OCLOperators addition
Created by Charles on 2025-11-18 · Last edited by Colline.ssali on 2026-01-22.
Symbol (+)
The + operator in OCL serves two primary purposes: numeric addition and string concatenation.
- When applied to numeric operands (Integer or Real), it performs arithmetic addition and returns a numeric result, promoting to Real if necessary.
- When applied to String operands, it concatenates them into a single string. The result type depends strictly on the operand types, ensuring type safety. This operator is widely used in calculations, derived attributes, and display expressions, making it one of the most common arithmetic and text-handling operators in OCL.
Example:
On Integer or Real numbers
5 + 3 -- returns 8
2.5 + 4.0 -- returns 6.5
2 + 3.5 -- returns 5.5 (Integer promoted to Real)On string
'Hello' + ' World' -- returns 'Hello World'Assume the Department class has numeric attributes PermanentStaff and ContractStaff.
Department.allInstances->collect(d | d.PermanentStaff + d.ContractStaff)