Skip to content

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
Playground
// 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

Reference · Source program

zolo run examples/features/65-type-projections.zolo --no-cache

Global index

Find your way through Zolo

Try an idea

Start here

9 results

9 results

enespt-br