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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

What Makes the Zig Programming Language Unique?

128 点作者 socialdemocrat超过 2 年前

20 条评论

belmarca超过 2 年前
<p><pre><code> Programming language experts told Andrew Kelley, the creator of the Zig programming language, that having code which could run at compile time was a really dumb idea. But he went ahead and implemented it anyway. Years later, this has proven to be one of the killer features of Zig. In the Zig world, we call it comptime, from the keyword used to mark code required to run at compile time or variables to be known at compile time. </code></pre> Which experts? The &quot;comptime&quot; is just macro-expansion from Scheme&#x2F;Lisp which has been around for a long time. Aren&#x27;t C++ templates also &quot;code that runs at compile time&quot;?
评论 #33104215 未加载
评论 #33104089 未加载
评论 #33107794 未加载
评论 #33103544 未加载
评论 #33104001 未加载
评论 #33104774 未加载
评论 #33103762 未加载
评论 #33107714 未加载
评论 #33103562 未加载
评论 #33106224 未加载
评论 #33104208 未加载
评论 #33106251 未加载
评论 #33126963 未加载
评论 #33103639 未加载
evil-olive超过 2 年前
I wrote a Sudoku solver (my go-to &quot;learn a new language&quot; project) in Zig and the compile-time features were extremely useful.<p>entirely at compile-time, I was able to generate lookup tables for &quot;given this cell index, give me the indexes of the cells in its row, column, and box&quot;. and I simply looped over that for each board size I wanted to support, from the standard 9x9 all the way up to 64x64.<p>another useful feature was support for arbitrary-sized ints [0] and packed structs [1]. for a 9x9 board, a cell can be represented with a 9-bit bitfield for possible values; a uint4 for the actual value; and a single bit indicating whether the value is known. Zig will happily pack that into a struct that occupies only 16 bits, and handle all the bit shifting &amp; masking for you when you access the struct fields.<p>this meant I was able to represent the state of a 9x9 board in just 162 bytes - a flat array of those packed structs. and the exact same code could operate on the ~28kb of state needed for 64x64.<p>0: <a href="https:&#x2F;&#x2F;ziglang.org&#x2F;documentation&#x2F;0.9.1&#x2F;#Primitive-Types" rel="nofollow">https:&#x2F;&#x2F;ziglang.org&#x2F;documentation&#x2F;0.9.1&#x2F;#Primitive-Types</a><p>1: <a href="https:&#x2F;&#x2F;ziglang.org&#x2F;documentation&#x2F;0.9.1&#x2F;#packed-struct" rel="nofollow">https:&#x2F;&#x2F;ziglang.org&#x2F;documentation&#x2F;0.9.1&#x2F;#packed-struct</a>
评论 #33103616 未加载
评论 #33117655 未加载
评论 #33104234 未加载
评论 #33103436 未加载
评论 #33104493 未加载
评论 #33104306 未加载
LAC-Tech超过 2 年前
- dealing with C level abstractions in a very ergonomic way<p>- ability to import and use C libraries, no FFI or custom bindings required<p>- clear &amp; logical distinctions between different pointer types and arrays<p>- allocators as a first class concept. this makes zig really safe because you can pass in test allocators that will report leaks etc.<p>If you enjoy hot takes and ancient memes, I made a talk about zig a couple of months ago where I lay out why I think it&#x27;s The Chosen One to succeed C.<p><a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=kCmOfRcmkN0" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=kCmOfRcmkN0</a>
gavinray超过 2 年前
&quot;In which people who have never used a language with compile-time expressions try Zig and think it&#x27;s novel&quot;<p>Not knocking Zig, I think it&#x27;s swell, but it wasn&#x27;t near the first language with this feature. D comes to mind, and C++ has it now with &quot;constexpr&quot; and &quot;consteval&quot;.
评论 #33104460 未加载
评论 #33104271 未加载
ctrlmeta超过 2 年前
&gt; Programming language experts told Andrew Kelley, the creator of the Zig programming language, that having code which could run at compile time was a really dumb idea.<p>Really? I can&#x27;t believe that! Running code at compile time is as old as Lisp! And it is present in some form or other in some other popular programming languages too. Like constexpr and templates in C++.
评论 #33109260 未加载
billfruit超过 2 年前
Zig tutorials and learning materials and on-boarding experience needs a lot of work. Compare with something like Kotlin, so well designed is its introductory material that one could start doing useful things with Kotlin within a few hours.<p>Also I find Zig&#x27;s choice to use abbreviated keywords rather cryptic, for example using &#x27;fn&#x27; instead of &#x27;function&#x27; only hurts readability I think.
评论 #33105585 未加载
评论 #33103861 未加载
评论 #33103926 未加载
评论 #33103919 未加载
评论 #33103765 未加载
dolmen超过 2 年前
Here is at least one thing that Andrew said:<p>&gt; There are a lot of people in this thread saying that I said stuff I didn&#x27;t say. It&#x27;s maddening.<p>Source: <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=33107359" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=33107359</a>
socialdemocrat超过 2 年前
Compile time code execution is implemented in a really neat way in Zig enabling anything from generics to meta programming.
p0nce超过 2 年前
Saying your product were first to do X, when by all measures it wasn&#x27;t first to do X, is the oldest play in the book of marketing.
password4321超过 2 年前
The whole &quot;Zig doesn&#x27;t have warnings&quot; thing didn&#x27;t go over well last month.<p><a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=32752383#32752885" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=32752383#32752885</a><p>I bought into the focus on the build toolchain, until I found out my old Mac wasn&#x27;t supported.
JohnDeHope超过 2 年前
I&#x27;d be perfectly happy if the answer to TFA&#x27;s question was &quot;nothing&quot;. Zig has the minimalism of something like C89 or Lua. It has a syntax like C. It has compile time execution (as per TFA) like all sorts of languages mentioned in the comments. Etc. There isn&#x27;t really anything particularly unique about it. Who cares? It feels to me like zig is an example of something that is greater than the sum of its parts.<p>Really the only thing I don&#x27;t think I&#x27;ve ever seen before is how zig passes around memory allocators. And that&#x27;s probably because I&#x27;m not a systems programmer by trade, so I&#x27;m less familiar with that sort of thing.
b3morales超过 2 年前
&gt; Zig will compile different variants of maximum for each case, where maximum is called with a different set of argument types<p>How does Zig handle the monomorphization problem for generics in library APIs? In other words, give the `maximum` function from the blog post, if I want to distribute a binary but make that function available to clients of my binary to call with different types, what does Zig do?
评论 #33152789 未加载
coldtea超过 2 年前
It&#x27;s lacklustre unicode string support. Pretty unique for a modern language!
评论 #33109507 未加载
mjan22640超过 2 年前
The &quot;Code run at compile time&quot; feature has been around since ages.<p>Haskell templates are in haskell, the &quot;compile time and run time language are the same&quot; feature is not unique either.
junon超过 2 年前
Terra has had this for a long time. Anything you wrote in Terra could be invoked at compile time just the same using LLVM&#x27;s JIT stuff. Made writing game tooling a breeze.
评论 #33121357 未加载
评论 #33107440 未加载
ecshafer超过 2 年前
I think Zig is pretty cool. I have played with it a bit. My main complaint I think is that the toolchain is pretty hard to get working right in windows, though its a breeze on linux. The only reason I would care about windows, is that I think it looks like a great language for game development since its fast, pretty easy to write, and interops with C&#x2F;C++ pretty easily.
efnx超过 2 年前
A sufficiently expressive (and safe) type system is Turing complete, and many languages have that. They don’t all feel ergonomic, but Haskell and Rust are pretty good. If you’re into it, you should check out Idris (lang) and Idris2. Both very cool languages that support dependent types (types that depend on values).
ReactiveJelly超过 2 年前
It&#x27;s the only way to cross-compile C and C++ code without losing hair follicles
malkia超过 2 年前
So how are loops in compile time avoided? Is there a timeout in the compiler, or N number of step&#x2F;instructions before bailing out?
评论 #33105522 未加载
keeeeeeeem超过 2 年前
I can&#x27;t speak to the quality of Zig&#x27;s compile-time compilation, other than to comment and say the article is taking liberties with its use of the word unique. As other commenter&#x27;s have noted, many languages have the feature. Instead I&#x27;m going to talk about a bunch of cool and weird compile-time code implemented by people much smarter than I.<p>If anyone reading this is interested in compile-time computation, you owe it to yourself to read Paul Graham (of HN)&#x27;s own work on compile time programming, &quot;On Lisp&quot;[0], or to take compile-time ideas to the next level, Doug Hoyte&#x27;s &quot;Let Over Lambda&quot;[1] (LOL). Lisp languages have a long and interesting history of compile-time computation via their various macro systems, and given the first-class inclusion in most Lisps, a greater variety of ideas have been explored regarding compile-time computation.<p>A few interesting examples:<p>LOL&#x27;s &quot;Pandoric macro&quot;[2] lets you monkey-patch closure values. It&#x27;s absolutely bonkers and would almost certainly be pathological to introduce to a codebase, but it&#x27;s an example of pushing the boundaries of what&#x27;s possible.<p>Common Lisp&#x27;s object system, implemented via macro[3]. To be honest, the entire Common Lisp language is a masterclass when it comes to learning about macros (I can&#x27;t avoid mentioning Scheme&#x27;s hygeinic macro system and Lisp-1&#x2F;Lisp-2.)<p>A special non-Lisp shout-out goes to Rust&#x27;s Diesel library for embedding SQL DDL queries into macros[4] which is not something I&#x27;ve personally seen before.<p>Clojure has a few interesting (and practical) macro libs, particularly core.async, which is an implementation of CSP (similar to Golang&#x27;s channels AFAIK), it embeds perfectly into the existing language and extends its capabilities even though it&#x27;s merely a library. Another interesting lib which comes to mind is Meander[5], which uses term-rewriting to provide transparent data transformations. Think declaratively interacting with data structures at compile time, and the library figuring out the best way of turning it into imperative value manipulation code.<p>[0] <a href="http:&#x2F;&#x2F;www.paulgraham.com&#x2F;onlisp.html" rel="nofollow">http:&#x2F;&#x2F;www.paulgraham.com&#x2F;onlisp.html</a> [1] <a href="https:&#x2F;&#x2F;letoverlambda.com&#x2F;" rel="nofollow">https:&#x2F;&#x2F;letoverlambda.com&#x2F;</a> [2] <a href="https:&#x2F;&#x2F;letoverlambda.com&#x2F;index.cl&#x2F;guest&#x2F;chap6.html#sec_7" rel="nofollow">https:&#x2F;&#x2F;letoverlambda.com&#x2F;index.cl&#x2F;guest&#x2F;chap6.html#sec_7</a> [3] <a href="https:&#x2F;&#x2F;mitpress.mit.edu&#x2F;9780262610742&#x2F;the-art-of-the-metaobject-protocol&#x2F;" rel="nofollow">https:&#x2F;&#x2F;mitpress.mit.edu&#x2F;9780262610742&#x2F;the-art-of-the-metaob...</a> [4] <a href="https:&#x2F;&#x2F;diesel.rs&#x2F;" rel="nofollow">https:&#x2F;&#x2F;diesel.rs&#x2F;</a> [5] <a href="https:&#x2F;&#x2F;github.com&#x2F;noprompt&#x2F;meander" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;noprompt&#x2F;meander</a>
评论 #33117610 未加载