While working on a program, I accidentally declared the variables in a struct incorrectly. The strange thing is that it actually worked and I'm not entirely sure why. I'm submitting this question to hopefully get some answers as to why this compiles and what the program is actually doing.
Here's the code of a short example program I made:
const std = @import("std");
//Standard Struct
const Regular = struct {
message: [:0]const u8 = undefined,
counter: u8 = 0,
pub fn init(self: *Regular) void {
self.message = "Hello from Regular";
self.incrementCounter();
}
fn incrementCounter(self: *Regular) void {
self.counter += 1;
}
pub fn print(self: *Regular) void {
std.debug.print("Counter: {}, Message: {s}\n", .{ self.counter, self.message });
}
};
//Weird Struct
const Weird = struct {
var message: [:0]const u8 = undefined;
var counter: u8 = 0;
pub fn init() void {
message = "Hello from Weird";
incrementCounter();
}
fn incrementCounter() void {
counter += 1;
}
pub fn print() void {
std.debug.print("Counter: {}, Message: {s}\n", .{ counter, message });
}
};
pub fn main() !void {
var a = Regular{};
const b = Weird;
a.init();
b.init();
a.print();
b.print();
var counter: u8 = 0;
while (counter < 20) : (counter += 1) {
a.incrementCounter();
b.incrementCounter();
}
a.print();
b.print();
}
Output:
Counter: 1, Message: Hello from Regular
Counter: 1, Message: Hello from Weird
Counter: 21, Message: Hello from Regular
Counter: 21, Message: Hello from Weird
Regular matches the standard struct architecture I've found in most examples from the documentation as well as Ziglings. Weird is this thing I accidentally made. Somehow this is still valid Zig and runs as I would expect, but there are a few quirks I don't quite understand.
For example, b cannot be a variable. If I attempt it, I get the error: "Error: variable of type 'type' must be const or comptime". Which is odd, since I'm expecting the type of b to be filename.Weird. Otherwise, it works how I would expect it. In the original program I discovered this in, I only discovered this by having to use a pointer to a Weird-type struct and finding numerous errors until I refactored it to a Regular-type struct. So what exactly is happening in Weird? Why does it work? How does it work?