TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Zig Quirks

256 pointsby andreabergiaabout 2 years ago

18 comments

the_mitsuhikoabout 2 years ago
&gt; The Zig documentation states that &quot;Identifiers are never allowed to &quot;hide&quot; other identifiers by using the same name.&quot;<p>I see this desire to get rid of shadowing all the time, but in practice it&#x27;s such a disruptive restriction.
评论 #35324268 未加载
评论 #35330056 未加载
评论 #35324850 未加载
评论 #35325278 未加载
评论 #35327249 未加载
评论 #35326528 未加载
评论 #35325769 未加载
habermanabout 2 years ago
&gt; Speaking of structure fields, they&#x27;re always public. Structures and functions are private by default with an option to make them public. But struct fields can only be public.<p>This feels like a mistake to me. I own and maintain a C library, and lack of private struct members is one of the things that causes the most problems. My users frequently reach into struct members even though I really do not want them to. This causes problems when those members have subtly different semantics than what they expect. It&#x27;s even worse when I want to change the internal representation, and I have to track down all my users&#x27; code and change it accordingly.<p>In C, my only options are:<p>(1) Define the structure in a .c file instead of .h. This works great in cases where I can take the performance hit of not being able to inline accesses of those struct members. But for performance-critical structures where I need to be able to inline, this is not an option.<p>(2) Make the member names something like private_foo, internal_foo, etc. and hope my users take the hint. But this also makes my own code more ugly and creates longer lines that are more likely to wrap.<p>Unfortunately Zig is even less capable than C in this regard, because Zig takes away option (1). Since there is no header&#x2F;source split in Zig, I cannot make a struct opaque by defining it in a source file only. Though I do see that there is &quot;opaque {}&quot;, perhaps that plus some casting could accomplish a similar thing?<p>I see that rationale for not having private struct members was given here: <a href="https:&#x2F;&#x2F;github.com&#x2F;ziglang&#x2F;zig&#x2F;issues&#x2F;9909#issuecomment-942686366">https:&#x2F;&#x2F;github.com&#x2F;ziglang&#x2F;zig&#x2F;issues&#x2F;9909#issuecomment-9426...</a><p>&gt; The idea of private fields and getter&#x2F;setter methods was popularized by Java, but it is an anti-pattern. Fields are there; they exist. They are the data that underpins any abstraction. My recommendation is to name fields carefully and leave them as part of the public API, carefully documenting what they do. [...] In my subjective experience, public fields generally lead to better abstractions by eliminating the temptation to attempt full encapsulation, when the more effective strategy is to provide composable abstraction layers.<p>Java does indeed represent an anti-pattern of verbose and frequently trivial getters and setters. But newer languages like C#, Swift, and Dart have more elegant and low-overhead syntax for properties, even allowing a property to move between being an actual field member and being derived, without breaking users.<p>As a library maintainer, full encapsulation is very important for evolution of a system over time. The only way for layers to truly be layers is if the contract at each layer boundary is clear. Otherwise the layers gel together and you cannot safely change any one layer independently.
评论 #35331570 未加载
评论 #35330014 未加载
评论 #35328270 未加载
评论 #35333769 未加载
评论 #35333881 未加载
评论 #35332528 未加载
评论 #35328207 未加载
adamrezichabout 2 years ago
&gt; Speaking of structure fields, they&#x27;re always public. Structures and functions are private by default with an option to make them public. But struct fields can only be public. The recommendation is to document allowed&#x2F;proper usage of each field.<p>&gt; I don&#x27;t want to editorialize this post too much, but it&#x27;s already caused the type of issues that you&#x27;d expect, and I think it&#x27;ll only cause more difficulties in a 1.x world.<p>I wish the author elaborated a bit more here because I&#x27;ve always been interested in what problem exactly `private` (and all the language complexity&#x2F;overhead) that comes with it solves. for something like C# it kind of makes sense because it&#x27;s a language built around chaining method calls and IntelliSense, so the user types `foo.` and IntelliSense shows literally everything you could want to do with that `Foo` instance, excluding `private` members&#x2F;methods. I can see the use-case for this in a corporate-type setting. in absence of this feature (which I <i>believe</i> is the case for Zig?), what possible difficulties can be caused, in practice?
ngrillyabout 2 years ago
Regarding naming conventions, there is an issue about switching to snake case for functions as well, the main rationale being there is no difference between snake_case and camelCase for one word identifiers:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;ziglang&#x2F;zig&#x2F;issues&#x2F;1097">https:&#x2F;&#x2F;github.com&#x2F;ziglang&#x2F;zig&#x2F;issues&#x2F;1097</a>
评论 #35337262 未加载
评论 #35325855 未加载
0x37about 2 years ago
I don&#x27;t usually like participating in bikeshedding, but the @ annotation feels like PHP&#x27;s $ to me in that I don&#x27;t see why it needs to exist. The language design could&#x27;ve easily just left that out and I don&#x27;t think anything would be lost.<p>Other than that, I&#x27;m definitely excited for Zig as a potential C++ replacement.
评论 #35324804 未加载
评论 #35324732 未加载
评论 #35324724 未加载
评论 #35327407 未加载
quietbritishjimabout 2 years ago
(Non-user of Zig here.)<p><pre><code> &#x2F;&#x2F; tea.zig full: bool = true, const Self = @This(); pub fn drink(self: *Self) void { self.full = false; } &#x2F;&#x2F; other.zig const Tea = @import(&quot;tea.zig&quot;); </code></pre> This example seemed to stop just short of the really interesting bit: what if other.zig called Tea.drink()? Would it set Tea.full to false? Maybe this is obvious to Zig users, but coming from C++, that would be a violation of const correctness.<p>In C++ (thinking of classes rather than files), you wouldn&#x27;t be able to call the drink method of a const object because it&#x27;s not marked as a const method. Or, if it was marked as a const method, you wouldn&#x27;t be able to modify full from in drink. You could get around this by marking full as mutable, which means you&#x27;re going to deliberately violate const correctnees, but at least you have to be explicit about it. (In theory mutable is meant for things like caches that don&#x27;t affect the visible behaviour of the class.)
评论 #35324466 未加载
评论 #35324507 未加载
rstarastabout 2 years ago
Re 9, would it be possible to change the signature of expectEqual from<p><pre><code> pub fn expectEqual(expected: anytype, actual: @TypeOf(expected)) !void </code></pre> to<p><pre><code> pub fn expectEqual(expected: @TypeOf(actual), actual: anytype) !void </code></pre> What would the downsides be?
评论 #35323953 未加载
评论 #35324422 未加载
评论 #35323932 未加载
scouttabout 2 years ago
Coming from C, I always see these &quot;Self&quot; variables&#x2F;types as counter-intuitive, and contribute to those cases where a function requires a parameter which I don&#x27;t have to supply (just for these functions accepting &quot;Self&quot;), which could be an exception instead of a rule. I would rather prefer the C++ &quot;this&quot; keyword usage.<p>Also, do &quot;Self&quot; kind-of variables occupy memory?<p>How can I declare a (packed) struct describing a payload in a way that the struct can be then sent &quot;as is&quot; as a network packet?<p>Let&#x27;s say this struct has a &quot;calculateChecksum()&quot; method. Will I need to declare &quot;Self&quot;? If so, will be &quot;Self&quot; part of the struct&#x27;s memory layout?
评论 #35327081 未加载
评论 #35325837 未加载
评论 #35333639 未加载
评论 #35326832 未加载
stephc_int13about 2 years ago
Some parts of the Zig syntax reminds me of the old K&amp;R style function declaration, that seemed absurd and was replaced at some point, but it was released, used in production and defended for a while.<p>There is often a strong rationale behind clumsy syntax decisions, I hope they will fix those quirks.
pjmlpabout 2 years ago
Well the amount of @ uses, it seems either to be to appeal to annotation heavy Java folks, or recovering Objective-C users.
评论 #35323876 未加载
评论 #35324294 未加载
评论 #35325880 未加载
nromiunabout 2 years ago
&gt; Functions are camelCase<p>&gt; Types are PascalCase<p>&gt; Variables are lowercase_with_underscores<p>Just why? It does not seem like the same language that used to treat tabs as a compiler error. Looks like the worst of every worlds to me. Why not just stick to one style?
评论 #35324816 未加载
评论 #35330849 未加载
评论 #35328004 未加载
评论 #35324780 未加载
评论 #35327462 未加载
lionkorabout 2 years ago
The biggest quirk is arrays vs slices, their notation, and pointer types. Its a nightmare
评论 #35324393 未加载
评论 #35324245 未加载
评论 #35327654 未加载
avinasshabout 2 years ago
why this wouldn&#x27;t be possible (or not preferred) without the dot?<p><pre><code> var tea = Tea{.full = true}; </code></pre> like:<p><pre><code> var tea = Tea{full = true};</code></pre>
评论 #35330370 未加载
评论 #35327589 未加载
rurbanabout 2 years ago
In my compiler I loosely coerce comptime_int&#x27;s to int&#x27;s if needed by subsequent API&#x27;s. You also need to expand or truncate it&#x27;s size on demand. E.g. C compilers do that.<p>zig just error&#x27;s, which is a silly behaviour.
nixpulvisabout 2 years ago
Correct me if I&#x27;m wrong, but it really seems like bad const inference that `x` from the 8th example is typed as a `comptime_int`. Is all comptime inference only done at the point of declaration?
ptatoabout 2 years ago
Please don&#x27;t do &quot;const Self = @This();&quot; if the type isn&#x27;t generic. It is pointless, redundant, ugly.<p>What issues could public struct fields have caused?
评论 #35324011 未加载
xchkr1337about 2 years ago
Are tabs supported yet?
评论 #35324414 未加载
评论 #35333458 未加载
frodowtfabout 2 years ago
Haven&#x27;t used Zig but that comptime coercion seems very annoying.