Generics
IonPath supports parameterized types for reusable data structures. Built-in generics include Maybe<T>, Array<T>, and Partial<T>.
Syntax
Generic types are declared with angle-bracket type parameters:
msg Response<T> { data: T; timestamp: datetime; success: bool; } msg Page<T> { items: T[]; total: u4; page: u4; }
Instantiation
Use generic types by providing concrete type arguments:
msg User { id: u4; name: string; } service UserService() { GetUser(id: u4): Response<User>; ListUsers(page: u4): Page<User>; }
Built-in Generics
The standard library (std feature) provides these built-in generic types:
Type Description
Maybe<T> Optional wrapper. Shorthand:
T?. C#: IonMaybe<T> Array<T> Dynamic array. Shorthand:
T[]. C#: IonArray<T> Partial<T> Sparse update tracker. Shorthand:
~T. C#: IonPartial<T> Maybe<T> (Optional)
Maybe<T> represents a value that may or may not be present. In C# it generates IonMaybe<T> — a Rust-style Option monad with implicit conversions:
// C# generated code IonMaybe<string> email = "user@example.com"; // implicit wrap IonMaybe<string> empty = default; // no value if (email.HasValue) Console.WriteLine(email.Value); // "user@example.com"
Partial<T> (Sparse Update)
Partial<T> tracks which fields have been modified. Only modified fields are serialized on the wire — ideal for PATCH-like updates:
// C# usage var patch = new IonPartial<User>(); patch.Set(u => u.name, "New Name"); // only 'name' will be sent on the wire