Hello there.
While I’m still learning about the language, I collected some questions I were bothering me past these days. I’m putting them in one thread to not spam more.
-
It’s known that using of unmodified vars is prohibited.
Let’s consider this use case:test "var pointer test" { var test_var: i32 = 1; var test_var2: i32 = 2; var test_ptr: *i32 = &test_var; test_ptr = &test_var2; }Here we assign two var variables to both var and const pointer. Zig should enforce the var assignment usage during the variable lifetime. Here, however, it skips the rule, and program passes the check. Same works for const
test_ptr.
If we replace pointer assignment with variable skip use, we get correct surface check for error of both var variable and var pointer:test "var pointer test" { var test_var: i32 = 1; var test_var2: i32 = 2; //error: local variable never mutated var test_ptr: *i32 = &test_var; //error: local variable never mutated // test_ptr = &test_var2; _ = test_var2; _ = test_ptr; }Therefore, I have a question:
Is it necessary to enforce mutability of a variable through a pointer?
If yes, how hard is it to implement and what bottlenecks are currently present for development of this feature?
Or this behavior is simply working as intended?Related issue: https://codeberg.org/ziglang/zig/issues/31049
-
At one of the Github discussions I noticed the mention of lambda implementation, and how it’s pure usage is forbidden in Zig due to it being not explicit enough. Here is my, quite ambiguous, take (from inspiration of possible implementation that also was mentioned in the thread):
const print = struct { fn self(comptime fmt: []const u8, args: anytype) void { return std.debug.print(fmt, args); } }.self;Here the question:
Is this construction viable for a real use case (and if yes, what is the proper example), or I should avoid writing my code like this due to there existing more optimal solutions?
-
I still can’t wrap my head around the concept of many-item pointers. I know they exist so we can point to an unknown amount of items in a buffer at runtime, But same we can workaround this limitation by using simple pointers.
How many-item pointers were created at the first place and what are use cases where we absolutely can’t do without them? Why can’t we chain these pointers?
Related question: Pointer Arithmetic / Offsets