TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Show HN: Zelda: Type-Safe Intrusive Linked Lists in Zig

3 点作者 samatman29 天前
There was a recent conversation[0] about the switch in Zig master branch to use of &#x27;intrusive&#x27; linked lists, in preference to the textbook kind.<p>These will usually have better performance, but the stdlib implementation comes with some disadvantages as well: the Node types are fully generic, so it&#x27;s possible to link two structs together which aren&#x27;t supposed to be in the same list. It uses @fieldParentPtr to retrieve the struct pointer, so if this sort of confusion happens, the result will probably be illegal memory access. Basically it pushes key operations out of the type system and onto the runtime, where they can cause trouble.<p>I&#x27;ve had in mind for a while a different approach, one which is type safe, so I wrote zelda. This is more of a comptime &#x27;mixin&#x27;, taking a struct type and adding node capabilities to it, constructing a SinglyLinkedList or DoublyLinkedList type for handling the lists themselves.<p>It&#x27;s a relatively straightforward translation of the stdlib types, passing a superset of the tests and adding a few additional operations and some diagnostic functions.<p>I had a lot of fun with it, and I think it answers some of the (fair) criticisms surfaced in the last conversation. It&#x27;s also a nice illustration of what comptime is capable of, in my opinion. If you like this kind of thing, check it out!<p>[0]: <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=43679707">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=43679707</a>

暂无评论

暂无评论