The Zig way is to not split. There’s nothing that corresponds to C#'s partial classes, which I assume is what you’re referring to. Unless you use a terrible editor, large files is a non-issue in Zig as long as the content logically belongs together.
t’s mostly a matter of taste — I don’t like having a lot of code in one source file.
It seems I can solve the problem by delegating parts of the dispatcher’s work to separate structs(separate files) ,
each with a pointer to the parent, and so having access to its “database” and functions.
Tbh, 500 lines is nothing. You should organize your source code by Zig modules, each in one file, and (IMHO) module files should have a good amount of meat in them, not artificially split into many small files.
But a few thousand lines per modules really isn’t a bad thing. It’s not the 1980s anymore where we had to split files into smaller ones because they didn’t fit into RAM
But if you want to split a big file into smaller files, then just move some features into separate .zig files and import them via const bla = @import("bla.zig"), but if you need to pass too much data into those ‘external’ functions it might be a sign that you have split into too small parts.
The question of how to split code into multiple files is completely orthogonal to the question of how to model inheritance or dispatching.
What exactly is your goal and what are you asking for?
In Zig you can both cram your entire program in one file or place every individual function in its own separate file. Because of how the language allows for circular imports/references between types there are very few restrictions. So if you have an aversion to 500+ line files* there’s nothing stopping you from organizing your code like this:
So you can organize code whichever way you want without being forced into using specific architectural patterns. I would recommend you to keep things as simple and straightforward as possible. Definitely avoid applying OOP-ish design patterns haphazardly if you can’t clearly explain and motivate to yourself why a particular pattern is the right choice for a given situation.
I think both are good tricks.
Folding is good when the code is very complex and you want to look at only a few different pieces of it and edit them in parallel, making it easier to look at the parts you are currently interested in and making more efficient use of whatever screenspace you have available (so especially helpful when working on small screens).
I think fuzz matching and jumping around is helpful when you can keep enough context in your head or have big enough screens so that you can put different parts of the code on different splits/views of the code etc. and when the need to jump around doesn’t exceed some level, where I would rather put all the relevant pieces of code on the screen at the same time, so I don’t have to jump around and build up context repeatedly.
Also another good use of folding is to temporarily get a sort of outline of the file, to get an idea about what are the overall contents of the file.
I also had a symbol outline in lazyvim which was also good for that, but that stopped working properly and I haven’t found out yet why it now only shows functions and not types, namespaces, etc.
If you haven’t thought about where this preference came from, it’s worth thinking about. I used to be averse to large files, because as a JavaScript developer, I was trained to break everything up as much as possible. It wasn’t until I learned Elm that I got over it.
Not to yuck your yum, if you want to keep your files small, go for it.
For anyone else that finds large files cumbersome, there’s lots of editor tricks you can use to make it easier.
Bookmarks can be useful, especially if your code isn’t broken down on an LSP level. (i.e. M, V, C, or css, html, scripts)
Jump to symbol will let you see all the available variables, methods etc in the file, and immediately jump to any of them.
Careful use of vim’s % key or treesitter enabled editors will let you jump to the next function, allowing you to quickly traverse files with lots of definitions in them.
Again, if you like small files, don’t let me stop you. Some languages have more influence on file sizes than others, but in general it comes down to how you like to abstract and how you separate (or don’t separate) your concerns.
Same here! It was very fortunate that I got introduced to this concept early on in my life. In JS-land, I found it very annoying to have to jump around files just to string things together mentally, because someone decided that one file = one function. Glad to see a talk that hits home how things should be structured.
Here is the talk from Elm creator for those interested: The life of a file
When you have a set of common functionality that operates on the same struct, enum, union, or opaque type, move the logic into the respective namespace.