Structs
In Flame, data structures and their behavior are cleanly decoupled: struct defines the data shape and memory layout, while impl defines associated functions and methods.
Defining a Struct
Section titled “Defining a Struct”Declare a struct with named, typed fields:
struct User { id: Int, name: String, email: String, is_admin: Bool}Instantiating Structs
Section titled “Instantiating Structs”You can create an instance of a struct by specifying its field values:
let user = User { id: 101, name: "Samantha", email: "samantha@example.com", is_admin: true}
print(user.name) // "Samantha"print(user.is_admin) // trueMutable Struct Instances
Section titled “Mutable Struct Instances”If you declare a struct variable with let mut, you can update its fields in-place:
let mut session = User { id: 1, name: "Guest", email: "guest@example.com", is_admin: false}
// Mutate fieldssession.name = "Admin"session.is_admin = true
print(session.name) // "Admin"Nested Structs
Section titled “Nested Structs”Structs can freely embed other custom structs, formulas, or vectors:
struct GeoLocation { lat: Float, lng: Float}
struct Company { name: String, location: GeoLocation, employees: Vec<User>}
let office = Company { name: "Flame Tech", location: GeoLocation { lat: 37.7749, lng: -122.4194 }, employees: [user]}
print(office.location.lat) // 37.7749Deserializing Structs
Section titled “Deserializing Structs”Flame provides built-in .fromJson() and .fromBytes() methods on all struct type definitions. This allows you to easily parse JSON strings or binary UTF-8 byte payloads directly into a typed struct instance.
struct Config { port: Int, host: String}
// Parse from a JSON stringlet cfg1 = Config.fromJson('{"port": 8080, "host": "localhost"}')print(cfg1.port) // 8080
// Parse from raw bytes (e.g., from an HTTP response or file)let raw_bytes = "{\"port\": 3000, \"host\": \"0.0.0.0\"}".toByte()let cfg2 = Config.fromBytes(raw_bytes)print(cfg2.host) // "0.0.0.0"Next Steps
Section titled “Next Steps”To add methods and constructors to your structs, explore Impl & Methods.
