# Coalesce Operator (??)
The **??** operator provides a concise way to handle default values. It returns the left-hand operand if it is **not** the default value for its type; otherwise, it returns the right-hand operand.
## Basic Usage
The syntax is `value ?? default`.
```pascal
var name: String := '';
PrintLn(name ?? 'Anonymous');
name := 'Alice';
PrintLn(name ?? 'Anonymous');
// OUTPUT
// Anonymous
// Alice
```
## Default Values by Type
The operator checks against the language-defined "empty" or "zero" state for each type.
| Type | "Empty" Condition |
| :--- | :--- |
| **String** | `''` (Empty string) |
| **Integer / Float** | `0` / `0.0` |
| **Object / Interface** | `nil` |
| **Boolean** | `False` |
| **Dynamic Array** | `nil` or length `0` |
| **Variant** | `Unassigned` or `Null` |
### Example: Integer and Floats
Note that `0` is considered the default value for numbers.
```pascal
var count := 0;
PrintLn(count ?? 10);
var price := 0.0;
PrintLn(price ?? 19.99);
// OUTPUT
// 10
// 19.99
```
### Example: Objects and Arrays
Highly useful for ensuring an object or array is initialized.
```pascal
var list: array of Integer;
// If list is empty/nil, returns a new array
var activeList := list ?? [1, 2, 3];
PrintLn(activeList.Length);
var obj: TObject := nil;
var instance := obj ?? new TObject;
PrintLn(instance.ClassName);
// OUTPUT
// 3
// TObject
```
## Chaining
You can chain multiple `??` operators to find the first non-default value.
```pascal
var input1 := '';
var input2 := '';
var input3 := 'Hello';
var result := input1 ?? input2 ?? input3 ?? 'Default';
PrintLn(result);
// OUTPUT
// Hello
```
## Short-circuiting
The right-hand side of a `??` expression is **only evaluated** if the left-hand side is the default value.
```pascal
function GetExpensiveDefault: String;
begin
PrintLn('Computing...');
Result := 'Heavy Value';
end;
var s := 'Quick Value';
// GetExpensiveDefault is NEVER called here
PrintLn(s ?? GetExpensiveDefault());
// OUTPUT
// Quick Value
```
## Use with JSON
The coalesce operator is particularly powerful when navigating JSON structures or dynamic data where fields might be missing or null.
```pascal
var data := JSON.Parse('{"id": 123}');
var title := data.title ?? 'Untitled'; // 'title' field is missing
PrintLn(title);
// OUTPUT
// Untitled
```
Coalesce Operator (??)
The ?? operator provides a concise way to handle default values. It returns the left-hand operand if it is not the default value for its type; otherwise, it returns the right-hand operand.
Basic Usage
The syntax is value ?? default.
var name:String:='';
PrintLn(name ?? 'Anonymous');
name :='Alice';
PrintLn(name ?? 'Anonymous');
Result
Anonymous
Alice
Default Values by Type
The operator checks against the language-defined "empty" or "zero" state for each type.
Type
"Empty" Condition
String
'' (Empty string)
Integer / Float
0 / 0.0
Object / Interface
nil
Boolean
False
Dynamic Array
nil or length 0
Variant
Unassigned or Null
Example: Integer and Floats
Note that 0 is considered the default value for numbers.
Highly useful for ensuring an object or array is initialized.
var list:arrayofInteger;// If list is empty/nil, returns a new arrayvar activeList := list ?? [1,2,3];
PrintLn(activeList.Length);var obj:TObject:=nil;var instance := obj ?? newTObject;
PrintLn(instance.ClassName);
Result
3
TObject
Chaining
You can chain multiple ?? operators to find the first non-default value.
var input1 :='';var input2 :='';var input3 :='Hello';var result := input1 ?? input2 ?? input3 ?? 'Default';
PrintLn(result);
Result
Hello
Short-circuiting
The right-hand side of a ?? expression is only evaluated if the left-hand side is the default value.
function GetExpensiveDefault:String;begin
PrintLn('Computing...');
Result :='Heavy Value';end;var s :='Quick Value';// GetExpensiveDefault is NEVER called here
PrintLn(s ?? GetExpensiveDefault());
Result
Quick Value
Use with JSON
The coalesce operator is particularly powerful when navigating JSON structures or dynamic data where fields might be missing or null.
var data := JSON.Parse('{"id": 123}');var title := data.title ?? 'Untitled';// 'title' field is missing
PrintLn(title);