Focus on debugging your application rather than debugging your programming language knowledge.
1 2 3 4 5 | {var i: u32 = 0; while (i < rects.len) : (i += 1) { // do the things... }} |
1 2 3 4 5 6 | // Joke proposal if (with(@as(usize, 0))) |*i| while (i.* <= 10) : (i.* += 1) { std.debug.print("{}\n", .{ i.* }); } // The serious proposal: with(var x: usize = 0) while(x < 10) : (i += 1) {...} |
1 2 3 4 | // C code: *ptr = sample; // Zig code: @ptrCast(*f32, @alignCast(@alignOf(f32), sample_ptr)).* = sample; |
1 2 3 4 5 6 7 8 9 10 11 | if (arena.used + size <= arena.base.len) { // This first attempt works, but what a mess... //var memory = @bitCast([]align(@alignOf(T))u8, arena.base[arena.used..]); //result = @ptrCast(*T, memory[0..@sizeOf(T)]); // Another way to do it! (please don't tell anyone) var address = @ptrToInt(arena.base.ptr + arena.used); result = @intToPtr(*T, address); arena.used += size; } |
knox
And the way I got this to work is probably seriously frowned upon for being unsafe.
1 2 3 | var address = @ptrToInt(arena.base.ptr + arena.used); result = @intToPtr(*T, address); arena.used += size; |
1 2 3 4 5 | const cur_buf = cur_node.data[@sizeOf(BufNode)..]; const addr = @ptrToInt(cur_buf.ptr) + self.state.end_index; const adjusted_addr = mem.alignForward(addr, ptr_align); const adjusted_index = self.state.end_index + (adjusted_addr - addr); const new_end_index = adjusted_index + n; |
1 2 3 4 | // C code: *ptr = sample; // Zig code: @ptrCast(*f32, @alignCast(@alignOf(f32), sample_ptr)).* = sample; |
notnullnotvoid
You're overstating your case by a lot with regard to unaligned accesses. Unaligned memory access has had little to no performance impact on x86 CPUs for about a decade, and ARM has had hardware support for unaligned accesses for at least that long, although the performance might still be significantly worse for all I know.
notnullnotvoid
Nevermind the absurdity of the claim that the language is nobly trying to warn you about the dangers of unaligned access on legacy ARM systems by making pointer casts a bit more tedious.
notnullnotvoid
it's just that Zig is more syntactically annoying about it because being superficially annoying is Zig's primary personality trait as a language.
stuff like SSE and AVX still has alignment requirements, what about those?
I would be thankful if a language pointed out such mistakes in compile time using static analysis
kristoff
What you're missing is precisely what the Zig type system was stressing you about: memory alignment.
1 | for(; i < 10; ++i) |
Okay, now you've piqued my interest. XD
What exactly do you find dishonest about a range [0-10)? Is it because you claim we should always use the length of a known range, and not hard code the length? I'm actually curious.
As an aside, I don't really see an issue with for (; i < 10; ++i)
; it simply looks to me like the programmer wants to loop over 10 elements of something.
But it brings up the point: This sort of thing is highly subjective, which is why there should never be any language bike shed policies imposed, and it is the reason why all BDFL languages are the Wrong Direction for the programming community (in my estimation).
This includes Jai.
The reason C became popular was because it grew--in a compression oriented fashion--out of existing code from dozens (or more?) of good programmers. All the new languages trying to compete with C are picking the wrong battles, and trying to be a top-down design based (for all practical purposes) on the opinions of a single programmer (the BDFL).
If we are truly being honest with ourselves, we would stop with the syntax/paradigm/orientation/"safety"/fad bike shedding, and move toward the simplest language that allows ideal metaprogramming/code generation facilities so the programmer, and not the language designer, can decide what color their own ******* shed will be.
Again, in my estimation.