🚀 Welcome to MDriven Learn –  MDriven is now on Discord!  Don’t miss the latest Release Notes.
OCLOperators sum
Created by Peter on 2019-12-18 · Last edited by Sandra.akech on 2026-02-07.

sum (): Real

Returns the sum of all elements contained in self if they support the '+' operation.

Expression Result
Sequence{2.3, 5.2} in c->sum() 7.5
Sequence{2, 4} in c->sum() 6
Sequence{2, '4'} in c->sum() invalid

The sum operator calculates the total sum of all numeric elements in a collection. It returns the aggregate sum with the same type as the collection elements.

Syntax

collection->sum() : T

Parameters

A collection of numeric values (Integer, Real, or Decimal)

ReturnType

T - The same numeric type as the collection elements:

  • Collection of Integers → returns Integer
  • Collection of Reals → returns Real
  • Collection of Decimals → returns Decimal


Example

Calculate total value of all orders:

Order.allInstances()->collect(o | o.Total)->sum()

Result:

0