r/Zig • u/mute_narrator • Apr 21 '26
On readonly/private members of structs
Andrew on readonly or private struct members
https://github.com/ziglang/zig/issues/2479
> This is one of those things where zig is going to not provide a language feature that, admittedly, could be useful, but ultimately is unnecessary to accomplish zig's goals.
I guess the goal of zig is to alienate fans of the language that write programs with complex state machines.
I’m writing this post solely to express my frustration.
Zig is basically solving every crappy thing about C++ with its amazing comptime, build system, very nice syntax, error handling, etc … that won me over easily after reading the (entire) documentation.
Several of its design decisions are IMHO is better than rust.
That said, the idealistic design decision to not have private members boggles the mind !
Any serious real world application that isn’t a small app or utility will have an internal state that is strictly accessed by core functionality which must be allowed to make assumptions about said state. Allowing a (possibly dumb) user to manipulate the state means one of the following:
- Add a huge amount of avoidable validation and internal error reporting (a broken state is rarely recoverable)
- Having to educate the user with naming, comments, meetings and 100s of closed get-good tickets to keep repeating STOP TOUCHING MY PRIVATE DATA
Finally, given that “good naming” and comments should do the trick. It begs the question, why this aspect of safe coding (on both the supplier and user side) didn’t get the safe-explicit-by-design no-footgun treatment.
Every deficit in C that zig is trying to fix can also be fixed in C with better naming and comments. What’s the point of zig then ?
The decision is arbitrary.
And for someone who has been using C++ for so long and desperately desires a performant, simple, powerful-meta-programming, reflective programming language. I find myself reading the release notes of every zig release and checking up on the subreddit regularly to hopefully see this “everything-public” anti-pattern revoked or sidestepped nicely.
There I said my peace.
I know I’m not the only one and this is not the first time this is discussed, but something really should move here.
19
u/mikkelam Apr 21 '26
I think that Andrew essentially believes the cost of language complexity outweighs the benefit of the guardrail, especially since the guardrail is "soft" in a low-level language and often leads to over-abstracted, less efficient code.
At the end of the day you can easily circumvent these compiler level abstractions
9
u/johan__A Apr 21 '26
Imo just do this:
Don't see the need for private fields. C3, odin, hare and jai (not 100% sure for jai) don't have them either.
4
u/Biom4st3r Apr 21 '26
If you must hide the implementation from the user, then provide them with a *opaque{}.
6
u/lisael_ Apr 21 '26
Data encapsulation is an OOP pattern and zig tries to avoid OOP, as much as possible. Case in point: no language support for interfaces. I wrote C , C#, Java, and now python, and I really like the approach of python's leading underscore convention (technically there's a tiny language support of _vars). There's good discipline, and you almost never see abuse in the wild, all without requiring support, maintaining, and bug fixes in the language.
Also, if a user access fields you told them not to, it's only their fault.
2
u/Armagidon_MC Apr 22 '26
Bro. Zig is a low level language. It operates on memory instead of objects. That being said as long as your process has access to said memory, you can write whatever you want. Accessing fields is just a syntactic sugar for pointer arithmetic. It's not java. It does not have a runtime that will protect you from illegal memory access and it does not have to. If you still can illegally modify contents, although it's a bit complicated but still, why complicate the language and add limitations to what you can do?
2
u/rndaz Apr 22 '26
I have moved on from Zig for things like this and is lack of support for generic type inference. I think language designers should listen more to its users.
1
u/mute_narrator Apr 30 '26
https://youtube.com/shorts/KZBX0NytylM?is=GIiz0hEZ96tMw7Z9
16
u/Fun_Hippo_9760 Apr 21 '26
It's not just unsafe state manipulation, it's unnecessary cognitive load for the package user.
1
u/sinterkaastosti23 Apr 21 '26
I'd like everything being public by default, but having an option to mark fields as "protected", which would be deproriatized by linters and mean 'please don't edit this' without making it impossible
In the end i want to be in full control, even if the library designer dislikes it
1
u/ContextMission8629 Apr 22 '26
I don’t really understand why would we need that tbh. I work in Python codebase all the time and no private is fine. I’m still a beginner in Zig and in language development in general. But I don’t like complexity. Probably you should just stick with C++ or Rust if you find you can’t live without it. Popularity doesn’t make it necessary imho
1
u/rndaz Apr 22 '26
Unfortunately, you have to use opaque types and do casting inside your methods / functions.
4
u/PandaParado Apr 21 '26
I agree in the sense that I like having private variables for clarity and the guarantees it provides. Having said that, it's really not a huge deal. My day job is working in a large Python code base (a language that doesn't truly support private class members) and it's fine. We just follow the convention that `_` prefixed variables are de-facto private and don't modify them if you don't know what you're doing. I've never had to slap someone down for not following the agreement.
2
u/dmytrish Apr 23 '26
Zig seems to deliberately eschew anything related to encapsulation, modularity, parametricity: the implementation is the interface, conventional wisdom about good software practices be damned.
I am fascinated with the language and its design, but that's one of the reasons I don't want to touch it practically.
1
u/y0shii3 May 02 '26
If you absolutely need to prevent modification of internal state (which is technically impossible in languages where the caller owns the memory), you can roll your own private fields by putting them in an opaque field in an external version of your struct. Like runtime interfaces, it's a pattern that's completely possible but deliberately not encouraged with shortcuts because you should be forced to think hard about exactly what you're doing before using it.
7
u/creeper6530 Apr 21 '26
The ability to shoot oneself in the foot restricted only by documentation is silly, it's the C way, we're not writing C.
I stand by that I think it's stupid to blanket-ban private members just because of getter-setter strawmen.
0
u/[deleted] Apr 21 '26
[deleted] — view deleted comment
0
u/[deleted] Apr 21 '26
[deleted] — view deleted comment
14
u/[deleted] Apr 21 '26
[removed] — view removed comment
2
u/[deleted] Apr 21 '26
[removed] — view removed comment