# POLL: Remove usingnamespace

**URL:** <https://ziggit.dev/t/poll-remove-usingnamespace/6131>\
**Category:** Brainstorming\
**Tags:** language\
**Created:** [September 25, 2024, 6:19am UTC](https://ziggit.dev/t/poll-remove-usingnamespace/6131 "2024-09-25T06:19:33Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![AndrewCodeDev](https://ziggit.dev/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@AndrewCodeDev](https://ziggit.dev/u/AndrewCodeDev)\
**Post date:** [September 25, 2024, 6:19am UTC](https://ziggit.dev/t/poll-remove-usingnamespace/6131/1 "2024-09-25T06:19:33Z")

</div>

This thread references the following issue on github: [Remove `usingnamespace` · Issue #20663 · ziglang/zig · GitHub](https://github.com/ziglang/zig/issues/20663)

Cast your vote here and consider explaining your thoughts!

_Poll ([view on site](https://ziggit.dev/t/poll-remove-usingnamespace/6131/1))_

---

<div class="post-metadata">

**Author:** ![AndrewCodeDev](https://ziggit.dev/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@AndrewCodeDev](https://ziggit.dev/u/AndrewCodeDev)\
**Post date:** [September 25, 2024, 6:24am UTC](https://ziggit.dev/t/poll-remove-usingnamespace/6131/2 "2024-09-25T06:24:43Z")

</div>

I’ll keep my thoughts brief here. I could go either way on `usingnamespace` but I am leaning towards removing it.

`usingnamespace` is a handy utility - it does a lot of work very quickly and it’s one of the most “intheritance-styled” things that we have in Zig. However, it’s more restrictive than inheritance in a good way. We can’t traffic in state with it in the form of injecting instance-level member variables.

The more time I’ve spent around implementation-injection facilities, the less I’ve come to like them. They are very useful from the perspective of an author, but as a reader I’ve often found them to make otherwise trivial refactors more annoying than they should be. They’re also a common source of sneaky bugs (probably less in the case of `usingnamespace` than in the case of full-fledged inheritance).

I’ve proposed an alternative way of injecting dependencies here: [Mixin alternative using const declarations](https://ziggit.dev/t/mixin-alternative-using-const-declarations/6091)

---

<div class="post-metadata">

**Author:** ![jeffective](https://ziggit.dev/user_avatar/ziggit.dev/jeffective/32/3116_2.png) [@jeffective](https://ziggit.dev/u/jeffective)\
**Post date:** [September 25, 2024, 7:00am UTC](https://ziggit.dev/t/poll-remove-usingnamespace/6131/3 "2024-09-25T07:00:42Z")

</div>

Recording my perspective as a beginner here(I voted I don’t know), before I forget what it’s like to a beginner.

My first and only experience with usingnamespace has been in reading about it in the documentation. I have not felt the need to use it after writing ~7000 lines of zig. But I also haven’t gotten to interfaces yet (and still don’t know what I am doing there yet after consuming a few hours of content).  
My first impression of it was that it was not immediately clear to me what it was. But i come from python so I didn’t really know what namespaces were for.

My second impression of it was that it severely destroys the readability of my code (in a similar manner to inheritance in python). Adding a single little line can drastically change the behavior / expectations of my program in an easily hidable way.

---

<div class="post-metadata">

**Author:** ![amesaine](https://ziggit.dev/user_avatar/ziggit.dev/amesaine/32/3233_2.png) [@amesaine](https://ziggit.dev/u/amesaine)\
**Post date:** [September 25, 2024, 8:14am UTC](https://ziggit.dev/t/poll-remove-usingnamespace/6131/4 "2024-09-25T08:14:04Z")

</div>

I have zero experience with usingnamespace but voted to remove it because it makes incremental compilation easier.

What Zig-pilled me is the fact that I serve the compiler so I can get better tooling, performance, and control. I’d like Zig to continue moving forward in that direction with this change.

It can be added back if it’s that much of a dealbreaker. And the factors surrounding that decision would be apparent once the much more complex language/toolchain features are fleshed out - i.e. when incremental compilation, async, and safe recursion are all combined.

---

<div class="post-metadata">

**Author:** ![JPL](https://ziggit.dev/letter_avatar_proxy/v4/letter/j/a9adbd/32.png) [@JPL](https://ziggit.dev/u/JPL)\
**Post date:** [September 25, 2024, 9:55am UTC](https://ziggit.dev/t/poll-remove-usingnamespace/6131/5 "2024-09-25T09:55:44Z")

</div>

I voted “I don’t know” because I didn’t need to use as in C++, finding in the use of complex structures enough solution.

---

<div class="post-metadata">

**Author:** ![pierrelgol](https://ziggit.dev/user_avatar/ziggit.dev/pierrelgol/32/10233_2.png) [@pierrelgol](https://ziggit.dev/u/pierrelgol)\
**Post date:** [September 25, 2024, 2:10pm UTC](https://ziggit.dev/t/poll-remove-usingnamespace/6131/6 "2024-09-25T14:10:33Z")

</div>

I have quite a bit of work to do because of this removal, but I’m not changing my stance. I’d rather work with a language where maintainers are willing to break things because they have a clear vision of the future, than a conservative language designed by committee that avoids breaking change, even when necessary out of fear that software engineers would have to work and actually fix their code. If they believe something should be removed, then so be it

---

<div class="post-metadata">

**Author:** ![mnemnion](https://ziggit.dev/user_avatar/ziggit.dev/mnemnion/32/2478_2.png) [@mnemnion](https://ziggit.dev/u/mnemnion)\
**Post date:** [September 25, 2024, 2:15pm UTC](https://ziggit.dev/t/poll-remove-usingnamespace/6131/7 "2024-09-25T14:15:45Z")

</div>

A poll was a bad choice here. People who are saying “I think it should be removed because other people think it should be removed” are not expressing an opinion, they’re engaging in a [Keynesian Beauty Contest](https://en.wikipedia.org/wiki/Keynesian_beauty_contest).

A poll removes the obligation to provide an informed opinion, and makes this thread noise, not signal. Polls are for languages designed by committee.

---

<div class="post-metadata">

**Author:** ![dee0xeed](https://ziggit.dev/letter_avatar_proxy/v4/letter/d/3ab097/32.png) [@dee0xeed](https://ziggit.dev/u/dee0xeed)\
**Post date:** [September 25, 2024, 2:19pm UTC](https://ziggit.dev/t/poll-remove-usingnamespace/6131/8 "2024-09-25T14:19:41Z")

</div>

Since I never used `usingnamespace`,  
(just because I did/do not clearly understand what it is for)  
I was going to mark “I don’t know”.

But after a while I wrote these two tiny files

```zig
// fancy-lib.zig
pub usingnamespace @import("std");

```

```zig
// program.zig
const foo = @import("fancy-lib.zig");
const log = foo.debug.print;

pub fn main() void {
    log("I'm fooling you - actually I came from 'std', not from 'fancy-lib'...\n", .{});
}

```

and after this I am thinking I do not need such a “re-exporting”,  
I’d better use `const properName = @import("some")`in every file  
where I need that “some”.

So I voted for removing.

P.S.

Also I looked into my `/usr/share/mc/syntax/zig.syntax` and…  
there is no `usingnamespace` in there, I just did not put this word into it.

---

<div class="post-metadata">

**Author:** ![AndrewCodeDev](https://ziggit.dev/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@AndrewCodeDev](https://ziggit.dev/u/AndrewCodeDev)\
**Post date:** [September 25, 2024, 2:20pm UTC](https://ziggit.dev/t/poll-remove-usingnamespace/6131/9 "2024-09-25T14:20:24Z")

</div>

I don’t think that’s the case - if you look at the github, you’ll see that there are similar metrics showing up on the opening post (unless we’re going to count both results as a social phenomenon).

On the contrary, I think we’re seeing more involvement because most people probably don’t have a strong enough opinion on the subject to motivate a writeup.

Also, I think people who are motivated will express an opinion regardless of a poll.

---

<div class="post-metadata">

**Author:** ![tw4452852](https://ziggit.dev/user_avatar/ziggit.dev/tw4452852/32/3402_2.png) [@tw4452852](https://ziggit.dev/u/tw4452852)\
**Post date:** [September 25, 2024, 2:22pm UTC](https://ziggit.dev/t/poll-remove-usingnamespace/6131/10 "2024-09-25T14:22:57Z")

</div>

Out of curiosity, is there any alternative way to achieve the following pattern w/o usingnamespace:

pub usingnamespace if (…) struct {} else struct { … }

---

<div class="post-metadata">

**Author:** ![AndrewCodeDev](https://ziggit.dev/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@AndrewCodeDev](https://ziggit.dev/u/AndrewCodeDev)\
**Post date:** [September 25, 2024, 2:23pm UTC](https://ziggit.dev/t/poll-remove-usingnamespace/6131/11 "2024-09-25T14:23:52Z")

</div>

Yep (and welcome to the community), here’s one I posted: [Mixin alternative using const declarations](https://ziggit.dev/t/mixin-alternative-using-const-declarations/6091)

---

<div class="post-metadata">

**Author:** ![biosbob](https://ziggit.dev/user_avatar/ziggit.dev/biosbob/32/1987_2.png) [@biosbob](https://ziggit.dev/u/biosbob)\
**Post date:** [September 25, 2024, 2:24pm UTC](https://ziggit.dev/t/poll-remove-usingnamespace/6131/12 "2024-09-25T14:24:07Z")

</div>

i’m still a relative newcomer to zig, so i’m not necessarily aware of some of “deep” implementation issues as well as all of the design alternatives…

having had a favorable experience with namespaces back in C++, i immediately knew the situations where this feature would be helpful…

based on the description in the docs (plus examples at this forum), it initially struck me as a very convenient way to merge disjunct source files with lots of top-level declarations… the alternative would have me explicitly creating a top-level file whose declarations are basically “renames” of internally imported declarations…

but as i progressed further in my design of **Zig•EM** i relied on `usingnamespace` in a rather novel manner… to illustrate, here’s an [example](https://blog.zigem.tech/post-003/#__codelineno-1-1) from a recent article of mine…

long story short, but each `.em.zig` source file is compiled **TWICE** in the **Zig•EM** framework: first with the file’s top-level declarations merged with the `EM __META` namespace; and then with the file’s top-level declarations merged with the `EM__ TARG` namespace…

[[read on in this article to understand the motivation]]

lacking `usingnamespace`, i could very easily generate **TWO** derivative source-files that respectively merge in the `EM __META` and `EM__ TARG` declarations… i’m not sure whether there is an alternative solution using other language constructs…

i’m not sure that the [Mixin](https://ziggit.dev/t/mixin-alternative-using-const-declarations/6091) alternative described by @AndrewCodeDev would apply here… a recent [topic](https://ziggit.dev/t/source-code-rewrite-transformations/6119/6) talking about “source code rewrite transformations” had @mnemnion suggesting a solution with `usingnamespace` 😉

in summary, it’s essential in my current framework design… but if i start doing some upstream source-code transformation anyway (for situations for which there is my only alternative), then i can easily subsume my current use-cases for `usingnamespace`…

---

<div class="post-metadata">

**Author:** ![AndrewCodeDev](https://ziggit.dev/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@AndrewCodeDev](https://ziggit.dev/u/AndrewCodeDev)\
**Post date:** [September 25, 2024, 2:26pm UTC](https://ziggit.dev/t/poll-remove-usingnamespace/6131/13 "2024-09-25T14:26:37Z")

</div>

Yes, my suggestion is not a direct replacement. `usingnamespace` is it’s own beast entirely and there are some cases that it won’t cover. For basic code deduplication it can serve much the same purpose.

To be clear, I’m not hard-line convinced it’s a bad thing, but I’m leaning away from it given my experience with implementation injection.

---

<div class="post-metadata">

**Author:** ![AndrewCodeDev](https://ziggit.dev/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@AndrewCodeDev](https://ziggit.dev/u/AndrewCodeDev)\
**Post date:** [September 25, 2024, 2:42pm UTC](https://ziggit.dev/t/poll-remove-usingnamespace/6131/14 "2024-09-25T14:42:59Z")

</div>

A post was split to a new topic: [How to use const declarations](https://ziggit.dev/t/how-to-use-const-declarations/6140)

---

<div class="post-metadata">

**Author:** ![Calder-Ty](https://ziggit.dev/user_avatar/ziggit.dev/calder-ty/32/10237_2.png) [@Calder-Ty](https://ziggit.dev/u/Calder-Ty)\
**Post date:** [September 25, 2024, 2:42pm UTC](https://ziggit.dev/t/poll-remove-usingnamespace/6131/15 "2024-09-25T14:42:15Z")

</div>

I should have read through the issue and the comments on it before voting. I voted for “I don’t have a preference”. Before this, i have never used `usingnamespace` nor really understood why it would be used.

After reading the issue, I lean more towards “remove”, for the possible benefits that it can provide for incremental compilation, and recognizing that I have had the problem of dealing with trying to find code that is re-exported in this way. However I also agree with the commenters that “readability” is not really a strong argument for removing it. Many of the examples of replacements are (in my opinon) less readable. However, I do understand that the code is more _discoverable_ in the new form.

> **My soapbox about readability**
>
> “Readability” of code is proportional to a person’s experience reading the kind of code in question. As more and more of the `mixin` pattern is proliferated, those with experience with zig code will find it more readable. So the main question with readabilty is “How obvious is the code”/“How easy is it to grok” for someone uninitiated.
> 
> As an example the use of `goto` in C for error handling and releasing resources is completely foreign to someone coming from python, and would be deemed “bad” code for readability. But someone who has seen the pattern 1000 times immediately understands what is going on.

I think `usingnamespace` can fall under a tool/syntax/semantic that makes some of these patterns more readable, but comes at a cost of complexity and discoverability.

It would be nice if after it is removed (if it is removed), we were able to bring in some of the functionality back, with less complexity.

---

<div class="post-metadata">

**Author:** ![dude\_the\_builder](https://ziggit.dev/user_avatar/ziggit.dev/dude_the_builder/32/557_2.png) [@dude\_the\_builder](https://ziggit.dev/u/dude_the_builder)\
**Post date:** [September 25, 2024, 3:04pm UTC](https://ziggit.dev/t/poll-remove-usingnamespace/6131/16 "2024-09-25T15:04:20Z")

</div>

Check out the linked issue under the section hading “Use Cases”. There are examples of alternative code for the most common `usingnamespace` use cases including implementation switching and mixins.

---

<div class="post-metadata">

**Author:** ![Dok8tavo](https://ziggit.dev/user_avatar/ziggit.dev/dok8tavo/32/707_2.png) [@Dok8tavo](https://ziggit.dev/u/Dok8tavo)\
**Post date:** [September 25, 2024, 3:09pm UTC](https://ziggit.dev/t/poll-remove-usingnamespace/6131/17 "2024-09-25T15:09:52Z")

</div>

I’v toyed around with making mixins both `usingnamespace` and `@fieldParentPtr`. The latter is more powerful, because it can contain context, it’s easy to pass the mixin interface as an argument, and it’s also more clear because of the possibility of conflicting declaration in multiple namespaces.

I don’t think anymore that `usingnamespace` is that good for making mixins.

---

<div class="post-metadata">

**Author:** ![mnemnion](https://ziggit.dev/user_avatar/ziggit.dev/mnemnion/32/2478_2.png) [@mnemnion](https://ziggit.dev/u/mnemnion)\
**Post date:** [September 25, 2024, 3:25pm UTC](https://ziggit.dev/t/poll-remove-usingnamespace/6131/18 "2024-09-25T15:25:48Z")

</div>

> [@AndrewCodeDev](#):
>
> Also, I think people who are motivated will express an opinion regardless of a poll.

I won’t. It’s the fruit of a poisoned tree. I have no interest in arguing the losing side of a ridiculous straw poll.

Congratulations. You know how many people want to press which button.

---

<div class="post-metadata">

**Author:** ![tgirod](https://ziggit.dev/user_avatar/ziggit.dev/tgirod/32/3744_2.png) [@tgirod](https://ziggit.dev/u/tgirod)\
**Post date:** [September 25, 2024, 4:45pm UTC](https://ziggit.dev/t/poll-remove-usingnamespace/6131/19 "2024-09-25T16:45:01Z")

</div>

Thank you for the keynésien beauty contest, that was an interesting read !

---

<div class="post-metadata">

**Author:** ![jeffective](https://ziggit.dev/user_avatar/ziggit.dev/jeffective/32/3116_2.png) [@jeffective](https://ziggit.dev/u/jeffective)\
**Post date:** [September 25, 2024, 5:02pm UTC](https://ziggit.dev/t/poll-remove-usingnamespace/6131/20 "2024-09-25T17:02:36Z")

</div>

> [@mnemnion](#):
>
> A poll removes the obligation to provide an informed opinion, and makes this thread noise, not signal

I’m not sure there was ever this obligation? I know this obligation exists on the GitHub issue tracker, because I recall Andrew K saying so on one of the long issues, but this is a forum.

Forums are noisy, forums are messy. We have some things to help cut through noise (like count, marking solutions) but this is a brainstorming post (no solution marking).

Beginners like myself come to the forum for the noise, we learn from the noise, we are born in it, molded by it (batman!). We are all fleshy LLMs anyway. Often learning is accelerated by doing it wrong first (making noise), and going back to fix it up (refactoring).

TLDR: the forum is attractive to beginners because it is messy and low-obligation. Nobody has to read the forum if they don’t want to. On the other end of the spectrum is the GitHub issue tracker, where we are requiring someone else to read something, and make a decision.

[Next page](https://ziggit.dev/t/poll-remove-usingnamespace/6131.md?page=2)
