Why doesn't zig have header files?

  1. Why doesn’t zig have header files?
  2. Why does C have header files?
  3. It seems like most modern languages don’t have header files, why?

C header files share common definitions and declarations between C files.
Also it was easy to implement #include as part of the C preprocessor.


Rob Pike explains the Go language design in: Go at Google: Language Design in the Service of Software Engineering. From the pain points and dependencies we can understand that the replacement of header files and includes by import statements have to do with faster compilation and controlling dependencies.

Zig tries to fix C problems; zig replaces the C preprocessor, see: The Road to Zig 1.0 - Andrew Kelley

2 Likes

Isn’t the burden of proof the other way around? Why have header files?

8 Likes

I guess the answer to all these question is simply “The C preprocessor”.

  1. Why doesn’t zig have header files?

There is no preprocessor or forward declaration requirements in Zig.

  1. Why does C have header files?

A long history of strict memory requirements that pushed C language designers to “single pass compiling”.

3 Likes
  1. Zig has a concept of modules
  2. C does not have a concept of modules. It uses headers to avoid code duplication. But they are not required, technically.

Bingo. It was brilliant at the time, given the constraints, but those constraints no longer apply today.

2 Likes

I could possibly use some advice here, because in my own projects, I seem to prefer header files for one main reason: documentation. When I go back to code I haven’t worked with recently, being able to skim the declared interfaces on a single screenful, without having to wade through implementation details is a real pleasure.

I guess some people use editors that can fold implementations, but I’ve never managed to get that to work right.

Regarding C, isn’t the main reason header files exist is that the language separates declaration from definition? Declarations have to be in scope at the call site, but definitions don’t need to be known until link time.

There is fold in (doom)emacs, works out of the box. Easy to call and read. Tho its more keyboard-oriented so theres no bullet to toggle in this one.

It has been stated that C doesn’t really have header files because they technically aren’t any different from source files. What C has are forward declarations. In my understanding, that is what really makes header files a thing.

1 Like

This really is true!

There’s a very specific way to use folding to make reading code much simpler, but it generally isn’t well supported out of the box. Everyone has “folding”, but only IntelliJ has a single checkbox to fold function bodies. nvim makes it relatively painless to do it yourself though https://old.reddit.com/r/neovim/comments/1g41rjy/can_neovim_do_this_already_with_treesitter/ls0cf4b/, though, for Zig specifically, some ingenuity would be required to come up with a useful tree sitter expression to filter out “generic types” which you don’t want to fold.

3 Likes