Struct - chung-leong/zigar GitHub Wiki

A struct defined in Zig can be instantiated like regular objects in JavaScript. The constructor expects an object containing initial values for each of its fields. Initializers can be omitted for those with default values.

pub const User = struct {
    id: u64,
    name: []const u8,
    email: []const u8,
    age: ?u32 = null,
    popularity: i64 = -1,
};
import { User } from './struct-example-1.zig';

const user = new User({
    id: 1234n,
    name: "Bigus Dickus",
    email: "[email protected]",
});
console.log(user.id);
console.log(user.name.string);
console.log(user.email);
console.log(user.age);
console.log(user.popularity);
1234n
Bigus Dickus
[email protected]
null
-1n

Analogous to the syntax of Zig, you can assign an object literal to a struct field and pass an object literal to a function expecting a struct as argument.

const std = @import("std");

pub const Address = struct {
    street: []const u8,
    city: []const u8,
    state: [2]u8,
    zipCode: u32,
};
pub const User = struct {
    id: u64,
    name: []const u8,
    email: []const u8,
    age: ?u32 = null,
    popularity: i64 = -1,
    address: ?Address,

    pub fn print(self: @This()) void {
        std.debug.print("Name: {s}\n", .{self.name});
        std.debug.print("E-mail: {s}\n", .{self.email});
        if (self.age) |age| {
            std.debug.print("Age: {d}\n", .{age});
        }
        std.debug.print("Popularity: {d}\n", .{self.popularity});
        if (self.address) |address| {
            std.debug.print("Street: {s}\n", .{address.street});
            std.debug.print("City: {s}\n", .{address.city});
            std.debug.print("State: {s}\n", .{address.state});
            std.debug.print("ZIP code: {d}\n", .{address.zipCode});
        }
        std.debug.print("---\n", .{});
    }
};
import { User } from './struct-example-2.zig';

const user = new User({
    id: 1234n,
    name: "Bigus Dickus",
    email: "[email protected]",
    age: 32,
    address: null,
});
user.address = {
    street: '1 Colosseum Sq.',
    city: 'Rome',
    state: 'NY',
    zipCode: '10001',
};
user.print();

// calling method like regular function
User.print({
    id: 1234n,
    name: "Bigus Dickus",
    email: "[email protected]",
    age: 32,
    address: {
        street: '1 Colosseum Sq.',
        city: 'Rome',
        state: 'NY',
        zipCode: '10001',
    },
});
Name: Bigus Dickus
E-mail: [email protected]
Age: 32
Popularity: -1
Street: 1 Colosseum Sq.
City: Rome
State: NY
ZIP code: 10001
---
Name: Bigus Dickus
E-mail: [email protected]
Age: 32
Popularity: -1
Street: 1 Colosseum Sq.
City: Rome
State: NY
ZIP code: 10001
---

Struct fields are non-enumerable. Passing a struct to Object.keys() or Object.entries() would produce an empty array. If you need to loop through a struct's fields programmatically, use its iterator, which returns key-value pairs in a fashion similar to that of Map.

const ResponseType = enum { normal, partial, bad };
const Response = struct {
    type: ResponseType,
    size: usize,
    code: u32 = 200,
    bytes: [8]u8,
};

pub fn getResponse() Response {
    return .{
        .type = .normal,
        .size = 512,
        .bytes = .{ 1, 2, 3, 4, 5, 6, 7, 8 },
    };
}
import { getResponse } from './struct-example-3.zig';

const response = getResponse();
for (const [ key, value ] of response) {
    console.log(`${key} = ${value}`);
}
type = normal
size = 512
code = 200
bytes = [object [8]u8]