You can use .Value in an OCL expression when you need a non-null numeric result from a nullable numeric value.
What .Value returns
.Value returns the underlying value of a nullable number. If the value has not been assigned, it returns 0.
Use it where an expression requires a number but the source attribute or calculated result may be null. This prevents a nullable numeric value from causing a null result when you need a numeric value for a calculation.
For example, if Price is nullable:
product.Price.Value- When
product.Priceis25, the expression returns25. - When
product.Pricehas no assigned value, the expression returns0.
Supported types
.Value is available for nullable numeric types.
| Type | Result when the value is null |
|---|---|
| Integer | 0
|
| Decimal | 0
|
| Double | 0
|
| Int64 | 0
|
Use .Value in totals
Apply .Value to the result of an aggregate when the total itself must be non-null.
For example, calculate the total price of all products:
Product.allinstances.Price->sum.ValueThis returns the summed price when a value is available and returns 0 when the result has no underlying value.
You can instead convert each product price before calculating the sum:
Product.allinstances->collect(i | i.Price.Value)->sumIn this expression, each product with an unassigned Price contributes 0 to the collection before sum is evaluated.
Choose where to apply it
| Requirement | Expression pattern | Example |
|---|---|---|
| You need a non-null value from one nullable numeric attribute. | Apply .Value to the attribute.
|
product.Price.Value
|
| You need a non-null aggregate result. | Apply .Value after the aggregate.
|
Product.allinstances.Price->sum.Value
|
Each nullable value must become 0 before an aggregate calculation.
|
Apply .Value inside collect.
|
i.Price.Value)->sum |
Check for null instead when null has meaning
Use .Value only when treating an unassigned number as 0 is correct for the rule. For example, an unassigned price and a price explicitly set to zero produce the same result after .Value.
If your rule must distinguish those cases, test the value with isNull or notNull before using it.
