Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove usingnamespace #20663

Open
Tracked by #13
mlugg opened this issue Jul 17, 2024 · 73 comments
Open
Tracked by #13

Remove usingnamespace #20663

mlugg opened this issue Jul 17, 2024 · 73 comments
Labels
breaking Implementing this issue could cause existing code to no longer compile or have different behavior. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@mlugg
Copy link
Member

mlugg commented Jul 17, 2024

This is a proposal to eliminate the usingnamespace keyword from the Zig programming language. I know this is a controversial one, but please read the whole thing before leaving any comments.

Overview

The usingnamespace keyword has existed in Zig for many years. Its functionality is to import all declarations from one namespace into another.

usingnamespace was formerly called use, and was actually considered for deletion in the past -- see #2014. Ultimately, Andrew decided not to remove it from the language, with the following main justifications:

  • pub usingnamespace @cImport(@cInclude(...)) is a helpful pattern. This point is made obsolete by move @cImport to the build system #20630.
  • Some parts of the standard library make use of usingnamespace. These uses have been all but eliminated -- this is discussed below.

In recent years, the usage of usingnamespace in first-party ZSF code has declined. Across the entire compiler, standard library, and compiler-rt, there is now precisely one usage of usingnamespace:

zig/lib/std/c.zig

Lines 35 to 48 in 9d9b5a1

pub usingnamespace switch (native_os) {
.linux => @import("c/linux.zig"),
.windows => @import("c/windows.zig"),
.macos, .ios, .tvos, .watchos, .visionos => @import("c/darwin.zig"),
.freebsd, .kfreebsd => @import("c/freebsd.zig"),
.netbsd => @import("c/netbsd.zig"),
.dragonfly => @import("c/dragonfly.zig"),
.openbsd => @import("c/openbsd.zig"),
.haiku => @import("c/haiku.zig"),
.solaris, .illumos => @import("c/solaris.zig"),
.emscripten => @import("c/emscripten.zig"),
.wasi => wasi,
else => struct {},
};

Every other grep result that shows up for usingnamespace is simply supporting code for it across the compiler pipeline. This leads us to revisit the decision to keep it in the language.

This is a proposal to delete usingnamespace from the language, with no direct replacement.

Why?

This proposal is not just to prune language complexity (although that is one motivation). In fact, there are three practical reasons I believe this language feature should be deleted.

Code Readability

A core tenet of Zig's design is that readability is favored over writability, because the majority of any programmer's time is inevitably spent reading code rather than writing it. Unfortunately, usingnamespace has a tendency to significantly harm the readability of code that uses it.

Consider, as an example, the singular usage of usingnamespace in the standard library. If I want to call the Linux libc function dl_iterate_phdr, where should I look to determine if this function is in std.c? The obvious answer would be std/c.zig, but this is incorrect. The usingnamespace here means that the definition of this function is actually in std/c/linux.zig! Similarly, if I want to discover which platforms this function is defined on, and how its signature differs between platforms, I have to check each file in std/c/*.zig, and compare the signatures between those files.

Without usingnamespace, the story would be completely different. dl_iterate_phdr would be defined directly in std/c.zig, perhaps with a switch per-target. All of the information about this function would be localized to the namespace which actually contains it: I could see, in the space of perhaps 10 lines, which platforms this function was defined on, and whether/how the signatures differ.

This is one example of a more general problem with usingnamespace: it adds distance between the "expected" definition of a declaration and its "actual" definition. Without usingnamespace, discovering a declaration's definition site is incredibly simple: you find the definition of the namespace you are looking in (for instance, you determine that std.c is defined by std/c.zig), and you find the identifier being defined within that type declaration. With usingnamespace, however, you can be led on a wild goose chase through different types and files.

Not only does this harm readability for humans, but it is also problematic for tooling; for instance, Autodoc cannot reasonably see through non-trivial uses of usingnamespace (try looking for dl_iterate_phdr under std.c in the std documentation).

Poor Namespacing

usingnamespace can encourage questionable namespacing. When declarations are stored in a separate file, that typically means they share something in common which is not shared with the contents of another file. As such, it is likely a very reasonable choice to actually expose the contents of that file via a separate namespace, rather than including them in a more general parent namespace. I often summarize this point as "Namespaces are good, actually".

For an example of this, consider std.os.linux.IoUring. In Zig 0.11.0, this type (at the time named IO_Uring) lived in a file std/os/linux/io_uring.zig, alongside many "sibling" types, such as SubmissionQueue. This file was imported into std.os.linux with a pub usingnamespace, resulting in namespacing like std.os.linux.SubmissionQueue. However, since SubmissionQueue is directly related to io_uring, this namespacing makes no sense! Instead, SubmissionQueue should indeed be namespaced within a namespace specific to io_uring. As it happens, this namespace is, well, the IoUring type. We now have std.os.linux.IoUring.SubmissionQueue, which is unambiguously better namespacing.

Incremental Compilation

A key feature of the Zig compiler, which is rapidly approaching usability, is incremental compilation: the ability for the compiler to determine which parts of a Zig program have changed, and recompile only the necessary code.

As a part of this, we must model "dependencies" between declarations in Zig code. For instance, when you write an identifier which refers to a container-level declaration, a dependency is registered so that if that declaration's type or value changes, this declaration must also be recompiled.

I don't intend to explain the whole dependency model in the compiler here, but suffice to say, there are some complexities. One complexity which is currently unsolved is usingnamespace. The current (WIP) implementation of incremental compilation essentially assumes that usingnamespace is never used; it is not modeled in the dependency system. The reason for this is that it complicates the model for the reasons we identified above: without usingnamespace, we can know all names within a namespace purely from syntax, whereas with usingnamespace, semantic analysis may be required. The Zig language has some slightly subtle rules about when usingnamespace declarations are semantically analyzed, which aims to reduce dependency loops. Chances are you have never thought about this -- that's the goal of those rules! However, they very much exist, and modelling them correctly in incremental compilation -- especially without performing a large amount of unnecessary re-analysis -- is a difficult problem. It's absolutely a surmountable one, but it may be preferable to simplify the language so that this complexity no longer exists.

Note that changing the language to aid incremental compilation, even if the changes are not strictly necessary, is something Andrew has explicitly stated he is happy to do. There is precedent for this in, for example, the recent changes to type resolution, which changed the language (by introducing the rule that all referenced types are fully resolved by the end of compilation) to simplify the implementation of incremental compilation.

Use Cases

This section addresses some common use cases of usingnamespace, and discusses alternative methods to achieve the same result without the use of this language feature.

Conditional Inclusion

usingnamespace can be used to conditionally include a declaration as follows:

pub usingnamespace if (have_foo) struct {
    pub const foo = 123;
} else struct {};

The solution here is pretty simple: usually, you can just include the declaration unconditionally. Zig's lazy compilation means that it will not be analyzed unless referenced, so there are no problems!

pub const foo = 123;

Occasionally, this is not a good solution, as it lacks safety. Perhaps analyzing foo will always work, but will only give a meaningful result if have_foo is true, and it would be a bug to use it in any other case. In such cases, the declaration can be conditionally made a compile error:

pub const foo = if (have_foo)
    123
else
    @compileError("foo not supported on this target");

Note that this does break feature detection with @hasDecl. However, feature detection through this mechanism is discouraged anyway, as it is very prone to typos and bitrotting.

Implementation Switching

A close cousin of conditional inclusion, usingnamespace can also be used to select from multiple implementations of a declaration at comptime:

pub usingnamespacee switch (target) {
    .windows => struct {
        pub const target_name = "windows";
        pub fn init() T {
            // ...
        }
    },
    else => struct {
        pub const target_name = "something good";
        pub fn init() T {
            // ...
        }
    },
};

The alternative to this is incredibly simple, and in fact, results in obviously better code: just make the definition itself a conditional.

pub const target_name = switch (target) {
    .windows => "windows",
    else => "something good",
};
pub const init = switch (target) {
    .windows => initWindows,
    else => initOther,
};
fn initWindows() T {
    // ...
}
fn initOther() T {
    // ...
}

Mixins

Okay, now we're getting to the big one. A very common use case for usingnamespace in the wild -- perhaps the most common use case -- is to implement mixins.

/// Mixin to provide methods to manipulate the `_counter` field.
pub fn CounterMixin(comptime T: type) type {
    return struct {
        pub fn incrementCounter(x: *T) void {
            x._counter += 1;
        }
        pub fn resetCounter(x: *T) void {
            x._counter = 0;
        }
    };
}

pub const Foo = struct {
    _counter: u32 = 0,
    pub usingnamespace CounterMixin(Foo);
};

Obviously this simple example is a little silly, but the use case is legitimate: mixins can be a useful concept and are used by some major projects such as TigerBeetle.

The alternative for this makes a key observation which I already mentioned above: namespacing is good, actually. The same logic can be applied to mixins. The word "counter" in incrementCounter and resetCounter already kind of is a namespace in spirit -- it's like how we used to have std.ChildProcess but have since renamed it to std.process.Child. The same idea can be applied here: what if instead of foo.incrementCounter(), you called foo.counter.increment()?

This can be elegantly achieved using a zero-bit field and @fieldParentPtr. Here is the above example ported to use this mechanism:

/// Mixin to provide methods to manipulate the `_counter` field.
pub fn CounterMixin(comptime T: type) type {
    return struct {
        pub fn increment(m: *@This()) void {
            const x: *T = @alignCast(@fieldParentPtr("counter", m));
            x._counter += 1;
        }
        pub fn reset(m: *@This()) void {
            const x: *T = @alignCast(@fieldParentPtr("counter", m));
            x._counter = 0;
        }
    };
}

pub const Foo = struct {
    _counter: u32 = 0,
    counter: CounterMixin(Foo) = .{},
};

This code provides identical effects, but with a usage of foo.counter.increment() rather than foo.incrementCounter(). We have applied namespacing to our mixin using zero-bit fields. In fact, this mechanism is more useful, because it allows you to also include fields! For instance, in this case, we could move the _counter field to CounterMixin. Of course, in this case that wouldn't be a mixin at all, but there are certainly cases where a mixin might require certain additional state, which this system allows you to avoid duplicating at the mixin's use site.

External Projects

This section will look over uses of usingnamespace in some real-world projects and propose alternative schemes to achieve the same effect.

Mach

I have discussed Mach's usages of usingnamespace with @slimsag in the past, and we agreed on migration mechanisms, so I won't go into too much detail here. A quick summary of a few of them:

  • math/ray.zig: this is redundant; the error case is already caught above.
  • math/mat.zig: this logic can be mostly generalized with some simple metaprogramming. What remains can be handled by the "implementation switching" approach discussed above or similar.
  • math/vec.zig: same as mat.zig.
  • sysaudio/wasapi/win32.zig: these are a little awkward for ABI reasons, but this is generated code so it's fine if it ends up a little verbose, and most of it is unused IIRC.

TigerBeetle

  • node.zig: this code uses usingnamespace to merge two namespaces. Probably these should just be imported separately, under tb and tb_client respectively.
  • testing/marks.zig: re-expose the 4 functions from std.log.scoped, and conditionally define mark.err etc as needed.
  • vsr/message_header.zig: namespaced mixins, as described above.
  • flags.zig: define main conditionally, as described above.
  • ring_buffer.zig: implementation switching, as described above.
  • tracer.zig: implementation switching, as described above.
  • lsm/segmented_array.zig: conditional inclusion, as described above.
  • clients/java/src/jni.zig: replace usingnamespace JniInterface(JavaVM) with const Interface = JniInterface(JavaVM), and replace JavaVM.interface_call with Interface.call (renaming that function).
  • clients/node/src/c.zig: to be replaced with build system C translation, see move @cImport to the build system #20630.

ghostty

I won't explain any specific cases here, since ghostty is currently in closed beta. However, I have glanced through its usages of usingnamespace, and it appears most could be eliminated with a combination of (a) more namespacing and (b) occasional manual re-exports.

@mlugg mlugg added breaking Implementing this issue could cause existing code to no longer compile or have different behavior. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. labels Jul 17, 2024
@mlugg mlugg added this to the 0.15.0 milestone Jul 17, 2024
@Snektron
Copy link
Collaborator

Snektron commented Jul 17, 2024

How do you suggest to remove the usingnamespace in c.zig? Seems to me the proposed suggestion leads to a lot of code duplication.

@mlugg
Copy link
Member Author

mlugg commented Jul 17, 2024

Good point, I should have addressed that. The proposed suggestion leads to relatively little duplicated code in reality. The bulky things are the definitions: those are just moved from c/*.zig to c.zig, resulting in a bigger but far more searchable file. If you take a look through e.g. std/c/linux.zig, there's actually really not much going on there:

  • ~100 re-exports from std.os.linux. These will be annoying to migrate, but you're not actually adding many total lines by doing so: const x = linux.foo just becomes .linux => linux.foo.
  • A few types defined in this file; same story as above.
  • A bunch of pub extern "c" fn definitions. I'm pretty sure many of these can just be in std.c unconditionally; after all, we already do this with things like the ptread_ and sem_ APIs. Those that actually need to be conditional will be done like std.c.getcontext or std.c.linux._errno today, or perhaps with @extern.

We have already started some parts of this migration. It'll certainly be tedious to complete, but I don't think it will make the std code worse.

Migrating this code is naturally a prerequisite for this proposal: so, if that migration goes poorly, it will of course give us a good reason to rethink this proposal.

@mikdusan
Copy link
Member

I propose instead of moving everything into a single lib/std/c.zig, we go with lib/std/os/NAME/c.zig; this will reduce the need for switch statements for things like pub const AT = and dispense with most if not all of the std.c.private namespace.

lib/std/std.zig

pub const c = switch (native_os) {
    .freebsd => os.freebsd.c,
    .linux => os.linux.c,
    .macos, .ios => os.darwin.c,
    else => ...,
};

and yes it is going to mean duplicating some extern "c" and other decls between os. But if there is sufficient need, those could be placed in a common_c.zig and re-decl'd in each os/NAME/c.zig where the same definition applies:

os/<NAME>/c.zig

const common = @import("../common_c.zig");

pub const read = common.read;
pub const write = common.write;

// things specific to this os
pub const AT = ...;

@BratishkaErik
Copy link
Contributor

I have read that proposal before in Discord (I think every time such comments were started by mlugg?) and complete proposal here. For now I have very negative opinion about removing usingnamespace, but before sharing my arguments I would like to hear what others think about this, and how would you (in general) suggest to migrate existing code in zalgebra and zig-libgccjit packages (std.c critique does not work here because they are all in the same scope as containing struct, and they help readibility because you can read all related grouped stuff instead of millions of switches/ifs):

https://github.com/kooparse/zalgebra/blob/1ab7f25cd3f947289e98a617ae785607bbc5054e/src/generic_vector.zig#L46-L51

https://git.sr.ht/~bratishkaerik/zig-libgccjit/tree/9d9f3d6e47c748b3649a3ec9665c6b8fc0ab9f15/item/src/wrapper.zig#L339

@mlugg
Copy link
Member Author

mlugg commented Jul 17, 2024

@BratishkaErik, here's an attempted translation of the usingnamespace in the first link. I don't have time to look at the second right now.

https://zigbin.io/cc01ce

  • init (formerly new; follow convention!) follows the "Implementation Switching" method described in the issue text.
  • {to,from}Vec{2,3,4} also follows that strategy, but does so in such a way that avoids duplicate code and groups all these conversions nicely.
  • z, w, zMut, wMut exist unconditionally; take advantage of lazy evaluation.
  • forward again follows the "Implementation Switching" strategy, with a @compileError fallback. Note that this isn't a function -- just use a declaration. (up should get the same treatment).

By the way, please stop marking everything as inline without a reason. All you're doing is hurting the compiler.

@aaal-dev
Copy link

The «mixins» example went more complicated to read. That breaks the idea about readability that you has mentioned.
Remove usingnamespace maybe not such bad idea. But there is some use-cases that could get worse. And you wrote an example by yourself. Maybe there need another keyword for case of type return. And not for struct @import

@BratishkaErik
Copy link
Contributor

See "Implementation Switching" in the OP.

I rewrite just 2 functions from zalgebra as how I understood Implementation Switching.
Before:

pub usingnamespace switch (dimensions) {
    2 => struct {
        pub inline fn new(vx: T, vy: T) Self {
            return .{ .data = [2]T{ vx, vy } };
        }

        pub inline fn toVec3(self: Self, vz: T) GenericVector(3, T) {
            return GenericVector(3, T).fromVec2(self, vz);
        }
    },
    3 => struct {
        pub inline fn new(vx: T, vy: T, vz: T) Self {
            return .{ .data = [3]T{ vx, vy, vz } };
        }
    },
    4 => struct {
        pub inline fn new(vx: T, vy: T, vz: T, vw: T) Self {
            return .{ .data = [4]T{ vx, vy, vz, vw } };
        }

        pub inline fn toVec3(self: Self) GenericVector(3, T) {
            return GenericVector(3, T).fromVec4(self);
        }
    },
    else => unreachable,
};

After:

pub const new = switch (dimensions) {
    2 => struct {
        inline fn new(vx: T, vy: T) Self {
            return .{ .data = [2]T{ vx, vy } };
        }
    }.new,
    3 => struct {
        inline fn new(vx: T, vy: T, vz: T) Self {
            return .{ .data = [3]T{ vx, vy, vz } };
        }
    }.new,
    4 => struct {
        inline fn new(vx: T, vy: T, vz: T, vw: T) Self {
            return .{ .data = [4]T{ vx, vy, vz, vw } };
        }
    }.new,
    else => unreachable,
};

pub const toVec3 = switch (dimensions) {
    2 => struct {
        inline fn toVec3(self: Self, vz: T) GenericVector(3, T) {
            return GenericVector(3, T).fromVec2(self, vz);
        }
    }.toVec3,
    3 => @compileError("toVec3 not defined for GenericVector(3, " ++ @typeName(T) ++ ")"),
    4 => struct {
        fn toVec3(self: Self) GenericVector(3, T) {
            return GenericVector(3, T).fromVec4(self);
        }
    }.toVec3,
    else => unreachable,
};

I personally see this as worse variant for both readability and compile errors.
Before, you had 1 switch with every function defintions in it, all grouped logically, you had detailed compile errors if user calls missing function:

src/generic_vector.zig:83:27: error: struct 'generic_vector.GenericVector(3,f32)' has no member named 'toVec3'
                _ = &Self.toVec3;
                          ^~~~~~
src/generic_vector.zig:33:19: note: struct declared here
    return extern struct {
           ~~~~~~~^~~~~~

you had less lines of code to read, and ZLS works perfectly here (go-to definition sends to the actual function, autocomplete works with correct doc comments etc.).
After, you have ~13 switches for every ~13 function, you have much worse compile errors (which you need to craft by yourself btw, so forget about machine-detecting, localization etc.):

src/generic_vector.zig:71:18: error: toVec3 not defined for GenericVector(3, f32)
            3 => @compileError("toVec3 not defined for GenericVector(3, " ++ @typeName(T) ++ ")"),
                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

, you need to scan every function to check what is the rule for inclusion of the function (maybe it switches not only on dimensions, etc.), you have ~13 verbose struct type declaration for every function.

If we'd had anonymous functions like in #4170 , it would regain some readibility (code would be ~same length as old code, and no structs-just-for-function), but will not fix compile errors.

@mlugg
Copy link
Member Author

mlugg commented Jul 17, 2024

@BratishkaErik, please see my translation linked above.

@hamish-milne
Copy link

const x: *T = @alignCast(@fieldParentPtr("counter", m));

I don't really use mixins myself, but if I had to use the above every time - both @fieldParentPtr which is kind of an esoteric feature, and @alignCast which reeks of un-safety - I'd hate it. The 'old' version of this looks much, much cleaner.

Encouraging better namespacing is a good goal, but I think a reasonable compromise here is to bite the bullet and just support struct composition, the pattern being:

const Bar = struct { _counter: u32 = 0 };
const Foo = compose(Bar, CounterMixin(Bar));

Or something.

This wouldn't even need a builtin, except that function declarations can't be generated/copied with reflection right now. If there was a field like impl: *anyopaque within std.builtin.Type.Declaration, you could remove the usingnamespace as dedicated syntax and simply implement it in the standard library.

@BratishkaErik
Copy link
Contributor

BratishkaErik commented Jul 17, 2024

I saw you responce just now, sorry!

* `init` (formerly `new`; follow convention!) follows the "Implementation Switching" method described in the issue text.
* {to,from}Vec{2,3,4} also follows that strategy, but does so in such a way that avoids duplicate code and groups all these conversions nicely.
  1. There's no deinit function, so it should not be named like that IMHO, it is misleading for a existing convention. 2) file-scope namespace is now polluted with a lot of functions which role is just to implement one function, you can call them accidentaly in tests in that file, your completion list is polluted by them (yes I know self parameter is not same, it will appear anyway when you write Self. etc.), it's harder to read implementation because you now need 2 go-to defintion commands (or if you are in the editor, two searches). It's also all dispersed on many-many lines. I can't notice how it is not less readable than older version.
    Also, again, compile errors are worse IMHO.
* `z`, `w`, `zMut`, `wMut` exist unconditionally; take advantage of lazy evaluation.

And now you have errors like "index 3 outside array of length 2" instead of more helpful "function is in fact not defined", also disadvantage in my book.

Note that this isn't a function -- just use a declaration. (up should get the same treatment).

I agree here, but it would be a breaking change.

By the way, please stop marking everything as inline without a reason. All you're doing is hurting the compiler.

Thank you. This code is very old so I don't remember why I put it here or if it was even myself, maybe even when stage1 was main compiler.

@BratishkaErik
Copy link
Contributor

@BratishkaErik, please see my translation linked above.

I saw this, but decided to finish my reply because did not want to lose comments by new bugged Github SPA logic or what they added to the site. Already had that experience before with review comments erased just because other comment reloaded itself.

@aaal-dev
Copy link

aaal-dev commented Jul 17, 2024

I need to clarify that the readability for other people could be not that you maybe assume. Generally speaking there is two type of developers. First type is usually write code in big functions with visual control of code. There is no option for them to break big function into small ones. That is because small functions require several jumps to see all code flow. And this is what they do not want to do. Second kind of developers is love small functions, but they also love functional style of coding. Zig has no any functional behaviors. And as I assume that Zig is C competitor. So we have problem here. C developers is much more the first type

@BratishkaErik
Copy link
Contributor

I don't really use mixins myself, but if I had to use the above every time - both @fieldParentPtr which is kind of an esoteric feature, and @alignCast which reeks of un-safety - I'd hate it. The 'old' version of this looks much, much cleaner.

I use this pattern a lot, IIRC all thanks to nektro, but 1) not in situration where usingnamespace would work better, and 2) while @fieldParentPtr do not appear in optimized generated code, alignCast still has runtime cost for checks in safe-enabled mode or scopes. I personally fix this by moving entire cast to separate function with @setRuntimeSafety(false), but can't speak for other people and whether it will be even worse for them.

@notcancername
Copy link
Sponsor Contributor

I am divided on this issue, but ultimately, I am leaning towards removing usingnamespace.

The removal of usingnamespace will make it more difficult to build cross-platform abstractions. The std.c example presented in the issue is significant because it highlights that providing alternative definitions for only some declarations requires either usingnamespace or a lot of code, as in:

const impl = switch(...) {...}; 

pub const foo = impl.foo;
pub const bar = impl.bar;
//...

Conditional inclusion is an important use case for usingnamespace. Unconditionally including a declaration that can't be used is inelegant.

The benefits of removing usingnamespace seem to outweigh the downsides, reducing the complexity of incremental compilation is a worthwhile goal. It's an unfortunate tradeoff.

@aaal-dev
Copy link

Case of usingnamespace switch and Conditional Inclusion creates a code that could went worse in some cases. Because of converting one fully controlled switch into the group of splitted switches looks like vulnerable behavior. Now there is a potential mistake to miss to change all splitted switches, and that mistake could take a place in much more simple way.

@travisstaloch
Copy link
Sponsor Contributor

travisstaloch commented Jul 17, 2024

Thanks @mlugg for doing this writeup. Without it I wouldn't have known how to do mixins without usingnamespace. I wanted to make sure it worked and that I understood so I removed usingnamespace from my vec.zig gist which uses mixins.

original with usingnamespace
updated without usingnamespace
difference

While these changes are less readable, I'm glad to know that this is still possible! 👍

It would be nice if this were a little simpler as now I must accept *const @This() and do @alignCast(@fieldParentPtr(...)) to get a *const Self. I did try this accepting @This() by value and referencing it, but that resulted in

/tmp/tmp.zig:26:71: error: pointer computation here causes undefined behavior
            const self: *const Self = @alignCast(@fieldParentPtr("m", &m));
                                                                      ^~
/tmp/tmp.zig:26:71: note: resulting pointer exceeds bounds of containing value which may trigger overflow

@mlugg
Copy link
Member Author

mlugg commented Jul 17, 2024

@hamish-milne

const x: *T = @alignCast(@fieldParentPtr("counter", m));

I don't really use mixins myself, but if I had to use the above every time - both @fieldParentPtr which is kind of an esoteric feature

What do you mean by "esoteric" here? @fieldParentPtr is a lovely feature of Zig, and used somewhat often.

and @alignCast which reeks of un-safety - I'd hate it. The 'old' version of this looks much, much cleaner.

You could align the mixin field so as to omit the @alignCast, but to be honest, the way I wrote it is just easier. You can easily wrap the nasty bit up in a helper function:

/// Mixin to provide methods to manipulate the `_counter` field.
pub fn CounterMixin(comptime T: type, comptime field_name: []const u8) type {
    return struct {
        const Ptr = *@This();
        fn get(m: Ptr) *T {
            return @alignCast(@fieldParentPtr(field_name, m));
        }
        pub fn increment(m: Ptr) void {
            get(m)._counter += 1;
        }
        pub fn reset(m: Ptr) void {
            get(m)._counter = 0;
        }
    };
}

pub const Foo = struct {
    _counter: u32 = 0,
    counter: CounterMixin(Foo, "counter") = .{},
};

It's worth noting that even in projects that use them, mixins are not commonly used: they are rarely appropriate in Zig. For instance, TigerBeetle has two "mixin-esque" types used with usingnamespace, and one of them (JniInterface) should not be a mixin at all.

Encouraging better namespacing is a good goal, but I think a reasonable compromise here is to bite the bullet and just support struct composition, the pattern being:

const Bar = struct { _counter: u32 = 0 };
const Foo = compose(Bar, CounterMixin(Bar));

Or something.

This almost certainly won't ever be a thing. Composing types is awkward in terms of defining things like field merging behavior, reconciling differing struct attributes, etc. It also really lacks general usefulness. If you're trying to compose two types, we have a convenient, intuitive, existing system for doing so: fields!

This wouldn't even need a builtin, except that function declarations can't be generated/copied with reflection right now. If there was a field like impl: *anyopaque within std.builtin.Type.Declaration, you could remove the usingnamespace as dedicated syntax and simply implement it in the standard library.

Please refer to this comment for why this is not going to happen.

@mlugg
Copy link
Member Author

mlugg commented Jul 17, 2024

@BratishkaErik

I saw you responce just now, sorry!

* `init` (formerly `new`; follow convention!) follows the "Implementation Switching" method described in the issue text.
* {to,from}Vec{2,3,4} also follows that strategy, but does so in such a way that avoids duplicate code and groups all these conversions nicely.
  1. There's no deinit function, so it should not be named like that IMHO, it is misleading for a existing convention.

Things with init do not necessarily have a corresponding deinit; see std.BoundedArray, std.Random.DefaultPrng, std.Target.DynamicLinker, std.PackedIntArray, std.EnumSet, std.StaticBitSet...

  1. file-scope namespace is now polluted with a lot of functions which role is just to implement one function, you can call them accidentaly in tests in that file, your completion list is polluted by them (yes I know self parameter is not same, it will appear anyway when you write Self. etc.),

If you're worried about this, you can namespace them under internal or something, but I really don't see this as a problem.

it's harder to read implementation because you now need 2 go-to defintion commands (or if you are in the editor, two searches).

But OTOH, tooling can find the definition more reliably. Even if it can't do all of the work, the first goto-def is trivial, and the second can be done by hand (or... by eyes?) very easily.

It's also all dispersed on many-many lines.

The old implementation was spread across 36 lines, while the new implementation is spread across 41 lines. That is really not a significant increase. In addition, the logic is now all actually dense; previously, similar functions -- perhaps even functions which called one another (e.g. toVec4 for the 2D case was implemented in terms of fromVec2 for the 4D case) -- were quite far separated in the source code.

I can't notice how it is not less readable than older version.

  • Decreased indentation makes nesting easier to track
  • Lines achieving related tasks are grouped, e.g. the aforementioned dimension-conversion methods
  • You don't have to worry about a function potentially having a completely different definition 30 lines later

Also, again, compile errors are worse IMHO.

* `z`, `w`, `zMut`, `wMut` exist unconditionally; take advantage of lazy evaluation.

And now you have errors like "index 3 outside array of length 2" instead of more helpful "function is in fact not defined", also disadvantage in my book.

A "no such function" error is all well and good if you understand the implementation of the library, but is completely unhelpful if you're skimming the source code and "I can see the definition right there, why won't it work?", or if you're using some tooling like ZLS and its heuristics got the included methods wrong, etc. You can reasonably argue that the error for things like z is as bad as the missing-method error (although I think that one's actually pretty understandable; it's exactly what I would expect from e.g. my_vec2[2] in a language with operator overloading), but if you believe that, just introduce your own @compileErrors.

Note that this isn't a function -- just use a declaration. (up should get the same treatment).

I agree here, but it would be a breaking change.

By the way, please stop marking everything as inline without a reason. All you're doing is hurting the compiler.

Thank you. This code is very old so I don't remember why I put it here or if it was even myself, maybe even when stage1 was main compiler.

@rohlem
Copy link
Contributor

rohlem commented Jul 17, 2024

@BratishkaErik with the risk of restating something obvious,

I believe the code structure of the `usingnamespace` example in your comment can be completely preserved by simply explicitly listing all the re-exported declarations:

pub const DimensionImpl = switch (dimensions) {
    2 => struct {
        pub inline fn new(vx: T, vy: T) Self {
            return .{ .data = [2]T{ vx, vy } };
        }

        pub inline fn toVec3(self: Self, vz: T) GenericVector(3, T) {
            return GenericVector(3, T).fromVec2(self, vz);
        }
    },
    3 => struct {
        pub inline fn new(vx: T, vy: T, vz: T) Self {
            return .{ .data = [3]T{ vx, vy, vz } };
        }
    },
    4 => struct {
        pub inline fn new(vx: T, vy: T, vz: T, vw: T) Self {
            return .{ .data = [4]T{ vx, vy, vz, vw } };
        }

        pub inline fn toVec3(self: Self) GenericVector(3, T) {
            return GenericVector(3, T).fromVec4(self);
        }
    },
    else => unreachable,
};
pub const new = DimensionImpl.new; //still re-exported just as with usingnamespace
pub const toVec3 = DimensionImpl.toVec3; //still either re-exported or compile error if referenced -> analyzed

@ethernetsellout
Copy link

I controversially agree that using zero-sized fields to emulate mixins is an adequate replacement for status quo, though the @alignCast(@fieldParentPtr("counter", m)) itself does look a bit awkward. Regardless, we could argue over how that usecase could be accommodated, but I really hope it's not through usingnamespace. This feature has lead to many a game of "where did this declaration come from" and I'd be happy to see it removed.

Also wanted to add that I feel no envy for the work the compiler has had to do to support this feature for so long. The 'subtle rules' you mention do not sound fun to write code for or design around.

@slimsag
Copy link
Sponsor Contributor

slimsag commented Jul 17, 2024

I'm here just to express that I am greatly in favor of this proposal because almost every time I encounter usingnamespace in practice, it has led to much worse code readability.

Mach engine is not very affected by this proposal because we have had several initiatives to banish usingnamespace from our codebase (although it has snuck back in a few times), and every time we have removed it it has led to improved code readability.

Big 👍 from me.

@nektro
Copy link
Contributor

nektro commented Jul 17, 2024

both the "Implementation Switching" and "Mixins" alternatives do not create better code imo.

The alternative to this is incredibly simple, and in fact, results in obviously better code: just make the definition itself a conditional

this would be inordinately tedious and having to reproduce the conditional for every decl would either make copy/pasting it error prone or force it to be a local decl which feels silly.

This can be elegantly achieved using a zero-bit field and @fieldParentPtr. Here is the above example ported to use this mechanism:

this is not a valid alternative to one of the main ways i use mixins. consider the following code from a web project im working on:

pub const User = struct {
    pub const table_name = "users";
    pub usingnamespace ox2.sql.TableTypeMixin(User);

    id: User.Id = .{ .id = 0 },
    uuid: ulid.ULID,
    provider: string,
    snowflake: string,
    name: string,
    joined_on: ox2.db.Time,
    last_updated: ox2.db.Time,
};

adding an extra field here is not possible because the type is a template for the database table specified by its table_name decl. additionally TableTypeMixin adds methods for interacting with said databse and this is included on all structs of this kind in the monorepo. some of the methods include:

const FE = std.meta.FieldEnum;
const FT = std.meta.FieldType;

pub fn byKey(alloc: std.mem.Allocator, comptime key: FE(T), value: FT(T, key)) !?T {
pub fn byKeyDesc(alloc: std.mem.Allocator, comptime key: FE(T), value: FT(T, key)) !?T {
pub fn byKeyCount(alloc: std.mem.Allocator, comptime key: FE(T), value: FT(T, key)) !u64 {
pub fn byKeyAnd(alloc: std.mem.Allocator, comptime key: FE(T), value: FT(T, key), comptime key2: FE(T), value2: FT(T, key2)) !?T {
pub fn byKeyAll(alloc: std.mem.Allocator, comptime key: FE(T), value: FT(T, key), comptime ord: Order) ![]T {
pub fn byKeyAndAll(alloc: std.mem.Allocator, comptime key: FE(T), value: FT(T, key), comptime key2: FE(T), value2: FT(T, key2), comptime ord: Order) ![]T {
pub fn update(self: *T, alloc: std.mem.Allocator, comptime key: FE(T), value: FT(T, key)) !void {
pub fn deleteBy(alloc: std.mem.Allocator, comptime key: FE(T), value: FT(T, key)) !void {

as i mentioned these methods are shared on every table struct of this kind so the readability is actually enhanced here, the usingnamespace means i dont have to copy/paste a decl definition into every type when a new method is added, and these operations make querying and updates extremely type safe and database backend agnostic.

i would make it through if usingnamespace was removed, but as it stands i very much enjoy using it in this way and feel it is a very expressive construct that strikes a balance between power and tedium when used sparingly.

@mlugg
Copy link
Member Author

mlugg commented Jul 17, 2024

this would be inordinately tedious and having to reproduce the conditional for every decl would either make copy/pasting it error prone or force it to be a local decl which feels silly.

The condition is usually just something like builtin.target.os. If it's more complicated, I think it's perfectly reasonable to have a separate (non-pub) value deciding the implementation used or whatever.

this is not a valid alternative to one of the main ways i use mixins.

All of those functions other than update I would namespace as e.g. User.Db.byKey; this is a clear instance of "namespacing is good, actually". These functions are getting general information from (or storing general information to) a database, which is completely unclear from their names. We could name all the functions fromDbByKey or something, but that's the "pseudo-namespace" issue sneaking in: instead we use an actual namespace, and all of our problems go away. The Db namespacing is appropriate because this is a shared, special property of these functions: other functions on User (say, isLoggedIn) might perform interesting work ("business logic" so to speak), whereas these are specifically performing database accesses and no further work.

update falls under the same umbrella, but it's method-ey rather than function-ey. If there's just the one, perhaps user.dbUpdate; if there are multiple, then field-based namespacing (user.db.update) is appropriate. Again, namespacing such methods appropriately makes their role -- performing a database access, with no further context about the application, and no integrated logic -- clearer.

EDIT: I forgot to mention, you could combine these mechanisms easily:

pub const User = struct {
    pub const Db = ox2.sql.TableTypeMixin(User, "db", "users"); // btw, pass this name in rather than having a random decl lookup!
    db: Db = .{},

    // (real fields here)
};

And also, just to be clear, TableTypeMixin knows to ignore the db field wrt the database table structure, because it knows that this is the mixin field.

@nektro
Copy link
Contributor

nektro commented Jul 17, 2024

All of those functions other than update I would namespace as e.g. User.Db.byKey; this is a clear instance of "namespacing is good, actually".

i left it out for brevity but in context it is very obvious what this type is for because its accessed as db.User and i dont want to go against Avoid Redundant Names in Fully-Qualified Namespaces either.

here's some more code that illustrates how its currently used in context:
(this particular example is code in the handler for /{repo}/-/issues/{number})

    const issueno = std.fmt.parseUnsigned(u64, subpage, 10) catch return fail(response, body_writer, .not_found);
    const issue = try db.Issue.byKeyAnd(alloc, .repo, repoo.id, .number, issueno) orelse return fail(response, body_writer, .not_found);
    const all_labels = try db.IssueLabel.byKeyAll(alloc, .repo, repoo.id, .asc);

    switch (response.request.method) {
        .GET => {
            const actions = try db.IssueAction.byKeyAll(alloc, .issue, issue.id, .asc);
            var name_cache = db.User.IdCache.init(alloc);

            const page = files.@"/issue.pek";
            const tmpl = comptime pek.parse(page);
            try response.headers.append("content-type", "text/html");
            try response.headers.append("x-page", "issue");
            try pek.compile(@This(), alloc, body_writer, tmpl, .{
                //...html template keys

And also, just to be clear, TableTypeMixin knows to ignore the db field wrt the database table structure, because it knows that this is the mixin field.

that could work but it adds a layer of indirection that i dont feel adds much value here other than being a clear workaround for the lack of usingnamespace

@zkburke
Copy link

zkburke commented Jul 18, 2024

I'm in favour of removing usingnamespace. Implementation switching can be even more robust than proposed (in my opinion) by switching on entire structs (each containing an implementation of every function) verses switching per function:

Take this example from one of my projects:

///Implementation structure
const Impl = switch (quanta_options.windowing.preferred_backend) {
    .wayland => @compileError("Wayland not supported"),
    .xcb => @import("xcb/XcbWindow.zig"),
    .win32 => @compileError("Win32 not supported"),
};

pub const Window = struct {
    //switched out at compile time of course
    impl: Impl,
    
    ///Some general documentation about the usage of this function
    pub fn implFunction(self: Window) void {
          return self.impl.implFunction();
    }
};

comptime {
     //do some validation of implementations maybe
}

test "Maybe even do some general behaviour tests" {

}

And if a function isn't defined on the struct, with adequate naming, you get the good error messages which tell you if a field or function isn't defined on an a particular implementation struct. It also means all the implementations are grouped together based on the platform (platforms are just one example but it applies to vector mathematics as well).

This is more verbose but it allows you to:

  • Document the general function, independently of the implementation.
  • Store different data per implementation, and store shared data in the parent struct if need be (or none if your just using free-standing functions,
    in which case this approach is really namespace switching rather than struct switching).
  • Have a single function which appears in LSP, with the aforementioned documentation.
  • If you need to take a peek in the implementation, it's all in one place and under one name.
  • Validate the implementations at compile time using the parent struct, enforcing that implementations provide implementation functions (and if they don't they can just @compileError(), you can figure out the rest of the static reflection).

This is my preferred way to do it, but it might be too cumbersome for some people's tastes, which I get.

@michaelbartnett
Copy link
Contributor

Occasionally, this is not a good solution, as it lacks safety. Perhaps analyzing foo will always work, but will only give a meaningful result if have_foo is true, and it would be a bug to use it in any other case. In such cases, the declaration can be conditionally made a compile error:

pub const foo = if (have_foo)
    123
else
    @compileError("foo not supported on this target");

Note that this does break feature detection with @hasDecl. However, feature detection through this mechanism is discouraged anyway, as it is very prone to typos and bitrotting.

I don't have any major complaints againsts removing of usingnamespace, but we need a way for @hasDecl and iterating through decls from a @typeInfo to not immediately raise a compile error in comptime code for cases like this example of how to do feature detection.

System feature detection with @hasDecl is demonstrably error-prone, but not all other uses of @hasDecl are buggy. How else do you provide optional customization points, such as in std.fmt and std.json? This also makes introspection utilities prone to compilation failures. The methods discussed in #6620 to resolve this satisfy all reasonable use cases IMO, but that proposal has yet to land on a concrete consensus & be accepted.

This is relevant to removing usingnamespace because I anticipate seeing much more of these show up in std and other libraries after the removal since it's a recommended idiom. I have already have issues with std.ArrayList(non-u8) and periodically vendor and patch array_list.zig to work with parts of my code.

@gwenzek
Copy link
Sponsor Contributor

gwenzek commented Jul 23, 2024

Thanks @mlugg for the proposal, detailed migration paths and peeks into the Zig internal.

About the conditional compilation, @usingnamespace is often used when working with conditional functions, because we don't have function literals in Zig: eg from tigerbeetle/flags.zig:

pub const main = if (@import("root") == @This()) struct {
    pub fn main() !void {
        std.debug.print("Hello world from flags !\n", .{});
    }
}.main else @compileError("main shouldn't be compiled in a library");

Could we get function literal to help make this use case more readable ?

Also in the case of conditional compilation, @usingnamespace is superior, because it allows to have ZLS only list the valid options. Could we have a builtin like @notImplemented to communicate the intent ?

@gwenzek
Copy link
Sponsor Contributor

gwenzek commented Jul 23, 2024

I'm also using usingnamespace for Mixins. Note that Zig is relatively bare bone in this domain having no "inheritance" or "trait". The usingnamespace does help with repetitive code.
Here is an example that use packed struct and usingnamespace to support different floating point types: https://zigbin.io/394bd7

The floating point types all share some basics functions like conversion to/from f32.
But some have nans, and some have inf and they aren't all encoded the same way.

I'm not sure if it make sense from a user perspective to have a x.methods.toFloat32(), but a x.isNan().

@gwenzek
Copy link
Sponsor Contributor

gwenzek commented Jul 23, 2024

Would it be possible to gut some features of @usingnamespace to make it easier to work with on the compiler side ?

Taking your example "Given the access A.x, which dependencies are necessary?"

  • we depend on the value of the second usingnamespace declaration
    -> only one @usingnamespace per struct is allowed

  • We also depend on the fact that there is no declaration x directly in A, since this would lead to an ambiguous reference error.
    -> allow @usingnamespace to define conflicting symbols. Always prefer the top level ones.

  • it won't consider certain usingnamespaces in field accesses to avoid dependency loops
    -> don't allow C to reference A if A is usingnamespace C

@IgorGRBR
Copy link

The provided "better pattern" for mixins example here seem to have some major drawbacks when it comes towards reading and interpreting the code by the user. When I'm reading something like foo.counter.increment(), I don't immediately expect it to modify the contents of foo by calling .increment() on something that looks like its component, at least since I've never saw foo pointer being passed to counter anywhere in code. Doing @alignCast(@fieldParentPtr("counter", m)); to effectively retrieve a pointer to a parent struct feels like a less-readable version of just passing parent pointer to counter struct IMO. But that would turn it into a component of a struct, which is a different concept from a mixin.

Personally I'm mostly in favor of removing usingnamespace if an alternative way of doing mixins. Perhaps a mixin keyword that (at least) mimics current behavior of usingnamespace in structs?

@mlugg
Copy link
Member Author

mlugg commented Jul 26, 2024

@IgorGRBR removing usingnamespace and adding in its place another keyword that does exactly the same thing in the common case of structs is barely even a change. How on earth do you think it would achieve any goals of the original proposal?

I want to stress that I don't think my proposed mixin system should be used all over the place: I maintain that mixins are rarely appropriate in Zig. When they are, my pattern sacrifices a little simplicity for a better API (& to be clear, I do think that where possible, state should be on the mixin field rather than the parent struct; it's cleaner and more reusable!) and drops a dependency on this complex language feature.

hazeycode added a commit to zig-gamedev/zig-gamedev that referenced this issue Jul 28, 2024
usingnamespace is a problematic language feature. See the proposal to remove it: ziglang/zig#20663
@cztomsik
Copy link

cztomsik commented Aug 5, 2024

When I'm reading something like foo.counter.increment(), I don't immediately expect it to modify the contents of foo by calling .increment() on something that looks like its component, at least since I've never saw foo pointer being passed to counter anywhere in code.

100% agree, it does something else than what I would expect, and it goes quite against the whole "being explicit about things" feature of Zig.

I can see why you want to remove usingnamespace and I'm generally not against but maybe we should stop calling such hacks as workarounds, and just accept that there is not going to be any alternative. (Being honest & predictable is also a feature)

@notcancername
Copy link
Sponsor Contributor

Notably, std.testing.refAllDeclsRecursive interacts badly with unconditional inclusion of declarations that @compileError on use.

@plaukiu
Copy link

plaukiu commented Aug 8, 2024

I don't immediately expect it to modify the contents of foo by calling .increment() on something that looks like its component, at least since I've never saw foo pointer being passed to counter anywhere in code. But that would turn it into a component of a struct, which is a different concept from a mixin.

the field can be renamed to make it clearer. in time, we may see a conventional qualifier for this arrangement emerge.

@mlugg
Copy link
Member Author

mlugg commented Aug 8, 2024

@notcancername since writing the original proposal, we've eliminated the usingnamespace in std.c, and tried out a slightly different convention for it: rather than @compileError, default things to {} on unsupported targets. You still get compile errors when attempting to use something unsupported, and you can do robust feature detection with @TypeOf(foo.bar) != void. And, yes, it plays nicely with refAllDeclsRecursive (although I'd really discourage anyone from ever using that function).

@michaelbartnett
Copy link
Contributor

@mlugg

And, yes, it plays nicely with refAllDeclsRecursive (although I'd really discourage anyone from ever using that function).

Even if we never call refAllDeclsRecursive, any code running over the decls of an arbitrary type non-recursively can trip over that. E.g. ArrayListAligned().Writer

Problem code:

zig/lib/std/array_list.zig

Lines 343 to 347 in ca012e5

pub const Writer = if (T != u8)
@compileError("The Writer interface is only defined for ArrayList(u8) " ++
"but the given type is ArrayList(" ++ @typeName(T) ++ ")")
else
std.io.Writer(*Self, Allocator.Error, appendWrite);

Do you think = {} is a good replacement for the current compile error there? I tried to make a similar change but was pointed to #6620 as the future fix for this.

To be clear, I'm not advocating for usingnamespace, although it does have the benefit of being able to conditionally make decls not even exist in a type as opposed to making them some sentinel value.

@cztomsik
Copy link

cztomsik commented Aug 9, 2024

@mlugg this ticket might be related (is_comptime fields are, to a certain degree, just a usingnamespace in disguise, and could be used for re-exports) but it's an edge-case so maybe not a problem?

#6709 (comment)

@plaukiu
Copy link

plaukiu commented Aug 9, 2024

When I'm reading something like foo.counter.increment(), I don't immediately expect it to modify the contents of foo by calling .increment() on something that looks like its component, at least since I've never saw foo pointer being passed to counter anywhere in code

It's not impossible to make this explicit. Here's a variant: obviously modifying, no need for @fieldParentPtr(), no need for a zero-sized field.

pub fn CounterMixin(comptime T: type, field_name: []const u8) type {
    return struct {
        parent: *T,

        pub fn increment(counter: @This()) void {
            @field(counter.parent, field_name) += 1;
        }
    };
}

const Foo = struct {
    field: usize,

    pub fn counter(_: Foo, self: *Foo) CounterMixin(@This(), "field") {
        return .{ .parent = self };
    }

//    dunno, this seems enough to me, you couldn't call `increment()` anyway
//    if the original wasn't `var`.
//    pub fn counter(self: *Foo) CounterMixin(@This(), "field") {
//        return .{ .parent = self };
//    }
};

pub fn main() !void {
    var foo: Foo = .{ .field = 0 };
    foo.counter(&foo).increment();

    const foo_counter = foo.counter(&foo);
    foo_counter.increment();

//    const foo_counter = foo.counter();
//    foo_counter.increment();

    std.debug.assert(foo.field == 2);
}

pub const User = struct {
    pub const table_name = "users";
//    pub usingnamespace ox2.sql.TableTypeMixin(User);

    id: User.Id = .{ .id = 0 },
    uuid: ulid.ULID,
    provider: string,
    snowflake: string,
    name: string,
    joined_on: ox2.db.Time,
    last_updated: ox2.db.Time,

    // is this not enough?
    pub fn as_table(self: *User) ox2.sql.TableTypeMixin(User) {
        return .{ .parent = self };
    }
};

@Mikastiv
Copy link
Sponsor

Mikastiv commented Sep 4, 2024

I've been using zigwin32 lately and I was wondering how I would do this without usingnamespace?

const win32 = struct {
    usingnamespace @import("win32").zig;
    usingnamespace @import("win32").foundation;
    usingnamespace @import("win32").system.memory;
    usingnamespace @import("win32").ui.windows_and_messaging;
    usingnamespace @import("win32").graphics;
    usingnamespace @import("win32").graphics.gdi;
};

Exemple from zigwin32: https://github.com/marlersoft/zigwin32gen/blob/main/examples/helloworld-window.zig#L1-L14

@andrewrk
Copy link
Member

andrewrk commented Sep 4, 2024

It seems like your code snippet and the win32 module disagree on the proper fully qualified namespaces for those symbols. I think you should just do const win32 = @import("win32"); and if you don't like the names, file an issue in the zigwin32 project.

@nektro
Copy link
Contributor

nektro commented Sep 4, 2024

there already exists @import("win32").everything to access all symbols from the same namespace as you would in C

@cztomsik
Copy link

cztomsik commented Sep 5, 2024

there already exists @import("win32").everything to access all symbols from the same namespace as you would in C

yes, but it's generated. so the real question is... given that this is valid use-case (because people sometimes want to use flat namespaces and some other people are willing to provide it, like win32.everything), do we really want to enforce build-system code-generation for re-exports?

another interesting example is std.testing: In bigger projects, I create a test-utils.zig file, where I re-export allocator, expectXxx() and my project-specific helpers... I do this with usingnamespace, and if it's gone, I will have to do it by hand.

  • writing it by hand does not feel good
  • writing fully qualified re-export t.t.expectEqual does not feel good either
  • importing both t = std.testing and t2 = @import("test-util") does not feel right either, but it's probably okish?
  • Is there a better option? I have no idea how other people do this and how they feel about it)

I think the argument for fully-qualified namespaces completely fades in the context of writing tests, because obviously std has unfair advantage here, it can add any helper into its own namespace, which you can't do your own projects.

BTW: I am not the only one doing the t shorthand, so I think the answer is not just simple "you're doing it wrong"
https://github.com/search?q=%22const+t+%3D+std.testing%22+lang%3Azig&type=code

EDIT: just to be clear, this is not about defending usingnamespace, I have mixed feelings about it too, but it IMHO solves some real problems. I personally think the root cause here is missing import syntax, with ability to rename and re-export symbols, but that is off-topic.

@notcancername
Copy link
Sponsor Contributor

The case of merging multiple namespaces into one is also much less readable without usingnamespace than it is with -- there's simply no easy way to tell where all the decls come from. Removing usingnamespace is more of a necessary evil to make the compiler simpler.

@paperdave
Copy link

paperdave commented Sep 5, 2024

importing both t = std.testing and t2 = @import("test-util") does not feel right either, but it's probably okish?

this seems reasonable to me, but i'd name the second one const u, or just test_util if it is not used frequently enough in the context it appears in.

@andrewrk
Copy link
Member

andrewrk commented Sep 5, 2024

util

https://ziglang.org/documentation/0.13.0/#Avoid-Redundancy-in-Names

Temptation to use "utilities", "miscellaneous", or somebody's initials is a failure to categorize, or more commonly, overcategorization. Such declarations can live at the root of a module that needs them with no namespace needed.

@zhuyadong
Copy link

zhuyadong commented Sep 6, 2024

I don't have any particular opinion on removing usingnamespace, but currently, usingnamespace is the only mechanism that can integrate methods from two structs into a third struct. If usingnamespace is removed without any alternative mechanism, my previous project zoop will not work and there will be no other way to make it work. It relies on the current mechanism as follows:

fn FuncList(comptime T: type) type {
  return struct {
    pub const items = .{
      struct {
        pub fn funcA(self: *T) void{}
      },
      struct {
        pub fn funcB(self: *T) void{}
      },
      struct {
        pub fn funcC(self: *T) void{}
      },
    };
  };
}

fn MergeStruct(comptime A: type, comptime B: type) type {
  return struct {
    pub usingnamespace A;
    pub usingnamespace B;
  };
}

fn Methods(comptime List: type) type {
  comptime var ret = struct{};
  inline for (List.items)|item| {
    ret = MergeStruct(ret, item);
  }
  return ret;
}

const MyStruct = struct {
  pub usingnamespace Methods(FuncList(@This()));
}

// now MyStruct have funcA/B/C

does anyone have a good solution for this problem?

update:
I have upgraded zoop and now no longer rely on usingnamespace, the new version is here

@AndreasHefti
Copy link

I heavily use usingnamespace for Mixins. It's very convenient and also readable in my opinion.

I don't like the suggested alternative for Mixins very much because its lacks readability and one would have to change a lot on the caller site as well as on the receiver side. This would lied to more boilerplate code as well.

If usingnamespace gets removed I hope at least there will be a good alternative for Mixins. Since Mixins are a well known concept I suggest just that:

pub fn CounterMixin(comptime T: type) type {
    return struct {
        pub fn incrementCounter(x: *T) void {
            x._counter += 1;
        }
        pub fn resetCounter(x: *T) void {
            x._counter = 0;
        }
    };
}

pub const Foo = struct {
    _counter: u32 = 0,
    pub mixin CounterMixin(Foo);
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking Implementing this issue could cause existing code to no longer compile or have different behavior. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Projects
None yet
Development

No branches or pull requests