File naming conventions for "export modules"

I’m new to Zig and curious if there’s any common naming conventions for modules that re-export structs, functions, etc. from a directory? In the standard library the convention seems to be:

foo/
  bar.zig
foo.zig

Where foo.zig contains pub const bar = @import("foo/bar.zig");. This would be fine if it weren’t for the fact that, if I rename something, I need to remember to do so in two places. Also, in file explorers, the “export module” (foo.zig) ends up pretty far away from the directory it exports things from.

I’ve taken to a pattern of:

foo/
  .zig
  bar.zig

Things wanting to import foo then do @import("foo/.zig"). This has a couple advantages:

  • Only one extra character vs foo.zig
  • Communicates the fact that this module exports things from the foo/ directory.
  • Ends up closer to modules in the foo directory in file explorers
  • Usually sorted at the top of directories in file explorers - foo/index.zig would get mixed in with all the other modules in the directory.

The main downside is that Unix-like OSs often hide . prefixed files by default (e.g. MacOS’s Finder). That said, most editors don’t do this, hence the reason I use this pattern. That could be solved using a leading underscore (_.zig) instead, but @import("foo/_.zig") feels ugly - probably because of conventions for using a leading _ as an indication that something’s private.

4 Likes

Welcome to ziggit!

The first convention you linked is the common / standard convention:

It is used by the standard library.

For example, there is json.zig and a json/ folder.

Note that whatever you choose, users outside if your library will not see these file names or folder names. Just declarations on structs. So they are both equivalent to a user who imports your module as a dependency using the build system.

Edit: well you already knew all this! sorry must have read too fast :slight_smile:

4 Likes

Rust has a number of conventions they can’t seem to choose from, perhaps one will be to your liking

  • they support the same that zig std does.
  • foo/mod.zig
  • foo/foo.zig

It’s worth considering that this is also a tooling problem on your side.

You can probably configure your editor to always show . prefixed files.
Your editor likely has a way to go to a file through a search, instead of a file explorer. If you navigate with a terminal, you also get this.
Perhaps you can configure your file explorer to put similarly names files and folders next to each other.

Perhaps some alternatives to what you are using support some of the above.

1 Like

To clarify, my editor (VSCode) shows dot files. Hence the reason the above pattern has been working for me.

I’ll give that a try.