valueOrDefault()

The valueOrDefault system function provides a way to retrieve the inner value of a nullable value type.

For example, a nullable Integer can contain either nil/null or a real Integer. valueOrDefault will return the real value, if one is assigned, or the default for the given type (0, in the case of an integer), if the nullable is not assigned.

An optional custom default value can be provided as second parameter.

Example:

var myNullable : nullable Integer;

var test1 := valueOrDefault(myNullable); // 0
var test2 := valueOrDefault(myNullable, -3); // -3
myNullable := 5; // value set

var test3 := valueOrDefault(myNullable); // 5
var test4 := valueOrDefault(myNullable, -3); // 5
int? myNullable;

var test1 = valueOrDefault(myNullable); // 0
var test2 = valueOrDefault(myNullable, -3); // -3
myNullable = 5; // value set

var test3 = valueOrDefault(myNullable); // 5
var test4 = valueOrDefault(myNullable, -3); // 5
var myNullable: Int?

let test1 = valueOrDefault(myNullable) // 0
let test2 = valueOrDefault(myNullable, -3) // -3
myNullable = 5 // value set

let test3 = valueOrDefault(myNullable) // 5
let test4 = valueOrDefault(myNullable, -3) // 5
int? myNullable;

var test1 = valueOrDefault(myNullable); // 0
var test2 = valueOrDefault(myNullable, -3); // -3
myNullable = 5; // value set

var test3 = valueOrDefault(myNullable); // 5
var test4 = valueOrDefault(myNullable, -3); // 5

See Also