09 · Data Structures
Types from selected fields
Define a response type with User.{ id, name, address.city as city }. Field types come from the original declaration. Missing, inaccessible or duplicate selected fields are rejected.
On this page
Define a response type with User.{ id, name, address.city as city }. Field types come from the original declaration. Missing, inaccessible or duplicate selected fields are rejected.
Complete example
Types from selected fields.
65-type-projections.zolo
// Public response types track the model without duplicating its declarations.
struct Address { city: str }
struct User { id: int, name: str, address: Address, internal_note: str }
type PublicUser = User.{ id, name, address.city as city }
fn label(user: PublicUser) -> str = "{user.name} — {user.city}"
let account = User {
id: 42,
name: "Ana",
address: Address { city: "Recife" },
internal_note: "billing contact",
}
let response = account.{ id, name, address.city as city }
print(label(response))
assert_eq(response.id, 42)
Reference
zolo run examples/features/65-type-projections.zolo --no-cache
See also