# Idiomatic code?

**URL:** <https://ziggit.dev/t/idiomatic-code/8762>\
**Category:** Help\
**Tags:** language\
**Created:** [February 27, 2025, 5:20am UTC](https://ziggit.dev/t/idiomatic-code/8762 "2025-02-27T05:20:40Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![lordnoriyuki](https://ziggit.dev/user_avatar/ziggit.dev/lordnoriyuki/32/4685_2.png) [@lordnoriyuki](https://ziggit.dev/u/lordnoriyuki)\
**Post date:** [February 27, 2025, 5:20am UTC](https://ziggit.dev/t/idiomatic-code/8762/1 "2025-02-27T05:20:41Z")

</div>

I’m interested in what the idiomatic way is to deal with deinit() requiring a non-const in cases like the code below. The code below won’t work, because the deinit function is `deinit(self: *Action, gpa: std.mem.Allocator)`

```zig
while (action_queue.popFront()) |action| {
    action.perform();
    action.deinit(gpa);
}

```

I can’t do `|*action|` in this case. My solution right now is to copy `action` to a var, as below:

```zig
while (action_queue.popFront()) |action| {
    action.perform();
    var a = action;
    a.deinit(gpa);
}

```

Is there anything better in terms of idiomatic zig? Ignoring the copy (which I hope gets optimized away, though I haven’t checked), I don’t even like having to come up with a unique variable name here just for this purpose.

Thanks!

---

<div class="post-metadata">

**Author:** ![Sze](https://ziggit.dev/user_avatar/ziggit.dev/sze/32/496_2.png) [@Sze](https://ziggit.dev/u/Sze)\
**Post date:** [February 27, 2025, 5:45am UTC](https://ziggit.dev/t/idiomatic-code/8762/2 "2025-02-27T05:45:55Z")

</div>

I guess you could change your queue to have methods `front` (where front would return a pointer to the first entry) and `removeFront`, then you could use this:

```zig
while (action_queue.front()) |action| {
    action.perform();
    action.deinit(gpa);
    action_queue.removeFront();
}

```

Otherwise if you want to keep using `popFront` I am not aware of a different solution than what you already found.

---

<div class="post-metadata">

**Author:** ![lordnoriyuki](https://ziggit.dev/user_avatar/ziggit.dev/lordnoriyuki/32/4685_2.png) [@lordnoriyuki](https://ziggit.dev/u/lordnoriyuki)\
**Post date:** [February 27, 2025, 5:52am UTC](https://ziggit.dev/t/idiomatic-code/8762/3 "2025-02-27T05:52:05Z")

</div>

Interesting - your version at least means I’m not worried about the copy or the additional variable that needs naming. It’s maybe ugly in a different way though since the logic of draining the queue is less obvious - without the removeFront() call it’d be an infinite loop.

---

<div class="post-metadata">

**Author:** ![lordnoriyuki](https://ziggit.dev/user_avatar/ziggit.dev/lordnoriyuki/32/4685_2.png) [@lordnoriyuki](https://ziggit.dev/u/lordnoriyuki)\
**Post date:** [February 27, 2025, 5:54am UTC](https://ziggit.dev/t/idiomatic-code/8762/4 "2025-02-27T05:54:51Z")

</div>

Another solution would be iterating over the backing array of the queue, calling `perform()` and `deinit()` on each and then resetting the queue to zero size at the end. This is a double ended queue, and using the internal backing array directly for this feels wrong. The iterator I have on the queue doesn’t help, since that returns elements, not pointers. I’d need an additional iterator that returns pointers. Maybe that’s least ugly of the options?

---

<div class="post-metadata">

**Author:** ![ForeverZer0](https://ziggit.dev/user_avatar/ziggit.dev/foreverzer0/32/2443_2.png) [@ForeverZer0](https://ziggit.dev/u/ForeverZer0)\
**Post date:** [February 27, 2025, 6:04am UTC](https://ziggit.dev/t/idiomatic-code/8762/5 "2025-02-27T06:04:37Z")

</div>

I know it is worthy of criticism, and it does feel dirty to do, but for `deinit` functions, I will actually use `@constCast` if it is necessary. The rationale I use is that the object is being destroyed, and it shouldn’t be considered valid after the call anyways.

This is of course very situational. The only time that I do this is if the type is otherwise capable of being `const` qualified for all of its other functionality.

---

<div class="post-metadata">

**Author:** ![Sze](https://ziggit.dev/user_avatar/ziggit.dev/sze/32/496_2.png) [@Sze](https://ziggit.dev/u/Sze)\
**Post date:** [February 27, 2025, 6:06am UTC](https://ziggit.dev/t/idiomatic-code/8762/6 "2025-02-27T06:06:04Z")

</div>

Maybe another approach would be to use a custom crafted iterator:

```zig
var iter = action_queue.consumeIterator(gpa);
while(iter.next()) |action| action.perform();

```

The iterator would be written in such a way that it returns the pointer to the first element and starting from the second iteration it would call `action.deinit(gpa);` `action_queue.removeFront();` before getting the pointer. That way the pointer would be valid for the whole body of the while loop. The last iteration that returns null would clean up the last action item.

I guess here the downside is having to create this custom iterator, but other than that I kind of like it.

---

<div class="post-metadata">

**Author:** ![lordnoriyuki](https://ziggit.dev/user_avatar/ziggit.dev/lordnoriyuki/32/4685_2.png) [@lordnoriyuki](https://ziggit.dev/u/lordnoriyuki)\
**Post date:** [February 27, 2025, 6:09am UTC](https://ziggit.dev/t/idiomatic-code/8762/7 "2025-02-27T06:09:37Z")

</div>

`@constCast` does neatly solve the problem, as dirty as it is. I’ve never used it before, and am now questioning why it’s called `@constCast` since it actually does the opposite. 😀

---

<div class="post-metadata">

**Author:** ![Sze](https://ziggit.dev/user_avatar/ziggit.dev/sze/32/496_2.png) [@Sze](https://ziggit.dev/u/Sze)\
**Post date:** [February 27, 2025, 6:19am UTC](https://ziggit.dev/t/idiomatic-code/8762/8 "2025-02-27T06:19:15Z")

</div>

> [@lordnoriyuki](#):
>
> and using the internal backing array directly for this feels wrong

I think it still is worth to try out that solution, maybe accessing the internal array should be seen as a valid use case, similar to how `.items` is part of the public interface for `std.ArrayList`, Zig doesn’t have private fields for a reason, so maybe this shouldn’t feel wrong at all.

Looping over the internal array processing all elements and then setting the size to zero, seems like the simplest approach that would likely have the best performance, because it doesn’t use single element functions to process it piece by piece. From a data-oriented programming stand point, iterating over the array seems like the best thing to do.

---

<div class="post-metadata">

**Author:** ![lordnoriyuki](https://ziggit.dev/user_avatar/ziggit.dev/lordnoriyuki/32/4685_2.png) [@lordnoriyuki](https://ziggit.dev/u/lordnoriyuki)\
**Post date:** [February 27, 2025, 6:23am UTC](https://ziggit.dev/t/idiomatic-code/8762/9 "2025-02-27T06:23:46Z")

</div>

I totally agree in general. In this case it’s a double ended queue, so isn’t actually guaranteed to have a single contiguous backing slice. That means you need to know a lot more about the internals of the queue than seems reasonable. Still doable, but uglier in this particular case than I’d like.

---

<div class="post-metadata">

**Author:** ![Sze](https://ziggit.dev/user_avatar/ziggit.dev/sze/32/496_2.png) [@Sze](https://ziggit.dev/u/Sze)\
**Post date:** [February 27, 2025, 6:35am UTC](https://ziggit.dev/t/idiomatic-code/8762/10 "2025-02-27T06:35:21Z")

</div>

What about adding a bulk processing api to the double ended queue, that gives you either an iterator (if the length isn’t known before iterating) or a slice over all the internal arrays?

In the most general case it could be something like this:

```zig
var iter = action_queue.consumeAll();
while(iter.next()) |chunk| {
    for (chunk) |*action| {
        action.perform()
        action.deinit(gpa);
    }
}

```

The iterator could than either remove whole chunk-arrays once they were processed (before the next iteration starts) or clear all arrays at the end, depending on what turns out better.

---

<div class="post-metadata">

**Author:** ![chung-leong](https://ziggit.dev/user_avatar/ziggit.dev/chung-leong/32/999_2.png) [@chung-leong](https://ziggit.dev/u/chung-leong)\
**Post date:** [February 27, 2025, 10:58pm UTC](https://ziggit.dev/t/idiomatic-code/8762/11 "2025-02-27T22:58:19Z")

</div>

That sort of begs the question, should `deinit()` accepts a regular pointer or a `const` pointer? Is freeing memory is an act of modification? When Thanos snapped his fingers, did Peter Parker become not-Spiderman? No, he just ceased to exist altogether. Being denied existence is not the same as being altered, I would argue.

---

<div class="post-metadata">

**Author:** ![squeek502](https://ziggit.dev/user_avatar/ziggit.dev/squeek502/32/409_2.png) [@squeek502](https://ziggit.dev/u/squeek502)\
**Post date:** [February 28, 2025, 12:17am UTC](https://ziggit.dev/t/idiomatic-code/8762/12 "2025-02-28T00:17:38Z")

</div>

> [@chung-leong](#):
>
> should `deinit()` accepts a regular pointer or a `const` pointer? Is freeing memory is an act of modification?

It’s typically done to allow poisoning the memory in an effort to make use-after-free easier to detect. One of many examples in the standard library:

> <https://github.com/ziglang/zig/blob/5c44934e20fedb29b88616f51de70c92e5d4ba42/lib/std/array_list.zig#L667-L670>

Can’t really speak to how effective it has been in practice. It’ll likely become much more relevant after [debug safety feature: runtime undefined value detection · Issue #211 · ziglang/zig · GitHub](https://github.com/ziglang/zig/issues/211) is implemented
