115 lines
1.9 KiB
Zig
115 lines
1.9 KiB
Zig
const std = @import("std");
|
|
const Other = @import("other.zig");
|
|
|
|
// Struct
|
|
|
|
const Vector2D = packed struct {
|
|
x: i5 = 2,
|
|
y: i23,
|
|
|
|
fn name(self: Vector2D) void {
|
|
_ = self; // autofix
|
|
|
|
}
|
|
|
|
fn init() void {}
|
|
fn deinit() void {}
|
|
};
|
|
|
|
// Functions
|
|
|
|
fn add(a: i32, b: i32) i32 {
|
|
return a + b;
|
|
}
|
|
|
|
fn setTo3(a: *i32) void {
|
|
a.* = 3;
|
|
}
|
|
|
|
// Error unions
|
|
|
|
const DivideError = error{
|
|
DivivisionError,
|
|
};
|
|
|
|
fn divide(a: f32, b: f32) !f32 {
|
|
if (b == 0) {
|
|
return DivideError.DivivisionError;
|
|
} else {
|
|
return a / b;
|
|
}
|
|
}
|
|
|
|
// Comptime generics
|
|
|
|
fn Optional(comptime T: type) type {
|
|
return struct {
|
|
value: T,
|
|
exists: bool,
|
|
};
|
|
}
|
|
|
|
// Tagged unions
|
|
|
|
const Person = struct {
|
|
name: []const u8,
|
|
};
|
|
|
|
const Horse = struct {
|
|
canRide: bool = false,
|
|
rider: ?*const Person = null,
|
|
};
|
|
|
|
const Duck = struct {
|
|
friend: ?*Animal = null,
|
|
gone: bool = false,
|
|
};
|
|
|
|
const AnimalType = enum {
|
|
Horse,
|
|
Duck,
|
|
Person,
|
|
};
|
|
|
|
const Animal = union(AnimalType) {
|
|
Horse: Horse,
|
|
Duck: Duck,
|
|
Person: Person,
|
|
};
|
|
|
|
pub fn main() !void {
|
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
|
const alloc = gpa.allocator();
|
|
defer _ = gpa.deinit();
|
|
|
|
var list = std.ArrayList(i32).init(alloc);
|
|
defer list.deinit();
|
|
try list.append(324);
|
|
try list.append(456);
|
|
|
|
for (list.items) |item| {
|
|
std.log.debug("item: {}", .{item});
|
|
}
|
|
|
|
var animal = Animal{ .Horse = .{ .canRide = true } };
|
|
|
|
const person = Person{ .name = "Dragutin" };
|
|
|
|
if (animal == .Horse) {
|
|
animal.Horse.rider = &person;
|
|
}
|
|
|
|
switch (animal) {
|
|
.Horse => |*horse| horse.rider = &person,
|
|
else => {},
|
|
}
|
|
|
|
var personAnimal = Animal{ .Person = .{ .name = "Aleksa" } };
|
|
|
|
var duck = Duck{ .friend = &personAnimal };
|
|
|
|
duck.gone = true;
|
|
|
|
std.log.debug("Rider name: {s}", .{if (animal.Horse.rider) |rider| rider.name else "none"});
|
|
}
|