4 ms·
I'm convinced, at this point, that Zig is that language, with comptime. Caveats: the system is not mature, Zig isn't 1.0 yet, there are some arbitrary limitatio
by samatman 2y ago
I'm convinced, at this point, that Zig is that language, with comptime. Caveats: the system is not mature, Zig isn't 1.0 yet, there are some arbitrary limitations, the comptime type system is somewhat all-or-nothing, TL;DR it isn't done. All of this is immaturity, rather than an essential limitation of the design.
The breakthrough with C++ templates, was to provide a macro system which creates an AST for a static language, at compile time, using (surprise!) a template of the desired code. Contrast with the C preprocessor, which works on the token stream (there's more to it if you're curious, but in a way that makes it worse, not better).
Of course, a macro system operating on the AST is an older idea than C, this is one of the essential features of Lisp. But Lisps have always been dynamic languages. In some other timeline, there was an s-expression language for writing static programs, which used Lisp-style macros, and thereby avoided the problems with C++ templates. But we don't live in that timeline, it's a road not taken.
This template system enabled C++ programmers to write code which was generic over not just types, but features of types, and much else, it's all rather complex.
The problem, in essence, is that it was thought that template-expansion could be a decidable process implemented using a sub-Turing-complete language. So it was built using a second syntax, which was intended to be restrictive but flexible. Uh-oh!
C++ templates are Turing complete, as it turns out. So what C++ has is a second language. You could write your entire program in that language, and execute it at compile time. For everything you can do in C++, there's a second way to do it in templates, with its own syntax and semantics. Invariably, this language is much more verbose, and just... weird, in big and small ways. The sum of this is a system which is difficult to understand, and which generates notoriously terrible errors. This isn't intended as a complete summary of C++ template's shortcomings and weird features. That would take longer.
Zig just lets you run Zig at compile time. It's much, much better. Instead of trying and failing to make the problem decidable, Zig brute-forces the question by providing a branch quota: this is similar to gas in the EVM, or what Piccolo, a Rust implementation of Lua, calls "fuel". Comptime comes with a complexity limit, after which it quits, and which can be boosted for operations where the quota is insufficient.
So there aren't two languages to learn, just the one: Zig. That glosses over some things, like loop and switch constructs have inline equivalents for comptime, but the general premise holds.
There are other things which C++ makes easy which are less easy in Zig, like runtime polymorphism. But comptime is just a massive improvement over templates.
- lioeters 2y agoOh that's a great response, much appreciated. I'm glad I asked the question. From what I understand, a disadvantage of this templating capability in C++ is that it makes the language "unpredictable" in terms of static analysis, so that, for example, intellisense in code editors cannot be implemented without having a full C++ compiler. ..But then again, maybe that's true of any language with macros, where the code can dynamically modify itself. That's an interesting point too, about how C++ templating is a second language that runs at compile time, making the code more complicated to understand. Whereas in Lisps, macros are written in the same language, working with the same AST in a unified way. It's conceptually simpler, a more elegant way to achieve the same result. I'd heard of comptime in Zig, but didn't know that there was a "complexity limit". Fascinating. It makes sense how it's related to the concept of "gas" in the Ethereum virtual machine, and "fuel" in Piccolo. (I remember seeing the latter language recently.) https://ziglang.org/documentation/master/#setEvalBranchQuota https://ziglang.org/documentation/master/#setEvalBranchQuota https://kyju.org/blog/piccolo-a-stackless-lua-interpreter/#fuel-pacing-and-custom-scheduling https://kyju.org/blog/piccolo-a-stackless-lua-interpreter/#f... https://ethereum.org/en/developers/docs/gas/ https://ethereum.org/en/developers/docs/gas/ ..Whew, many tempting rabbit holes there. :) I'd been interested in Zig, reading about it here and there, but your comment convinced me to get more serious about studying it. Thanks!
- brabel 2y agoInteresting that you seem to consider Zig a pioneer in comptime templating, when, as other commenter mentioned, D has been doing that for two decades. https://tour.dlang.org/tour/en/basics/templates https://tour.dlang.org/tour/en/basics/templates https://tour.dlang.org/tour/en/gems/compile-time-function-evaluation-ctfe https://tour.dlang.org/tour/en/gems/compile-time-function-ev... As far as I can tell, D's templates and CTFE are as powerful as Zig's comptime (not sure if they're exactly equivalently powerful, but if they're not, the difference is not very large in practice or I'm pretty sure I would've noticed).
- samatman 2y ago> As far as I can tell, D's templates and CTFE are as powerful as Zig's comptime You might very well be right. I didn't realize that D's templating was much different from C++, I don't know much about the language. I'll also note that the sibling comment you refer to wasn't posted when I wrote my reply. I'll note that a careful reading of my post, and the one it was responding to, will reveal that I didn't claim that Zig was the only, or first, language to improve on C++ templates. Only that it's an example of a more modern language which has done so. But I'm also largely ignorant of the ins and outs of D. It seems that you, however, are not. Here's an example of Zig building a data structure at comptime: https://ziglang.org/documentation/0.12.0/std/#src/std/comptime_string_map.zig https://ziglang.org/documentation/0.12.0/std/#src/std/compti... Is this an example of something D could do as well? If so, I'll be sure to give the language credit the next time the topic comes up.