8 ms·
Yeah, that's a bit of a turnoff. The rest of the article (until the paywall) was alright. Zig's use of comptime is fairly revolutionary in a c-like language.
by klyrs 4y ago
Yeah, that's a bit of a turnoff. The rest of the article (until the paywall) was alright.
Zig's use of comptime is fairly revolutionary in a c-like language. Compared to the shenanigans required by c++, the entire language* is (should be) available at comptime. Compared to the backtick/quote/macro magic available in lisp... the awesomeness isn't that it's comptime, the awesomeness is that it's type safe.
But, I'd say it isn't even the use of comptime that's particularly noteworthy, it's the lack of pretty much every feature that isn't comptime. This wouldn't be a good idea without comptime being as robust and featureful as it is.
* nope, see puffoflogic's reply
- bogwog 4y agoI think Haxe has pretty much the same feature, which it just calls "macros" (https://haxe.org/manual/macro.html https://haxe.org/manual/macro.html). I haven't used Zig (yet, maybe one day), but does anyone know if there's a difference between Haxe macros and Zig comptime? AFAIK Circle also offers something similar for C++ (https://www.circle-lang.org/ https://www.circle-lang.org/)
- curtisf 4y agoZig's comptime isn't like "macros" in Haxe or Rust or Scala or Lisp which transform one AST into another AST. It's just code that runs when the code is compiled, rather than when the resulting binary is run. This makes it less expressive, but it also means it is easier to learn and use. (It's a lot like C++'s constexpr) The interesting part comes from recognizing that by making constructs like structs first-class-values in comptime, you can achieve most of the common things that templates, macros, conditional compilation, etc give you, but with a much simpler feature (which is just running code at compile time)
- dleslie 4y agoLisp and Scheme macros are just code that happens to execute at runtime, too. They don't necessarily need to transform the AST, though that's what they're often used for. Like with Zig, you are limited mostly by what is available in the environment at macro expansion time; unlike Zig, you can do things that generate allocations.
- lispm 4y agoMacros in Lisp during compilation often extend the compile-time environment and another purpose is to register information in the Lisp IDE (record source code, record source locations, ...). > but with a much simpler feature (which is just running code at compile time) Common Lisp does that with the EVAL-WHEN construct, which allows to run arbitrary code at compile time.
- bogwog 4y ago> It's just code that runs when the code is compiled, rather than when the resulting binary is run. That’s exactly what Haxe macros are. It’s regular Haxe code that runs during compile, and it has access to all Haxe language features and the standard library. The only special thing about them is that they can directly manipulate the AST before it gets passed on to the compiler for final code generation. In a sense, they’re like “shaders” for the compiler.
- puffoflogic 4y agoVery little of the language is available at comptime. Allocation is unavailable and the zig developers have declared this a non-feature. That either locks out or requires redundant implementations of a great many things.
- anonymoushn 4y agoIs it the plan that FixedBufferAllocator will never be usable at comptime, or just allocators normally backed by syscalls?
- puffoflogic 4y agoFBA was usable last time I tried zig, but I don't count it because if you knew ahead of time how much you needed to allocate, well then you didn't need an allocator at all. I.e. I define "allocator" to mean "dynamic allocator". Note that the buffer provided to FBA would have to be provided at the very top. Fixed-size allocators are not composable. You cannot write any kind of comptime routine which calls further routines which need to do allocation, and how much allocation they do is based on arguments to your routine - except by forcing your caller to calculate how much allocation needs to be done. But that leaks your implementation details to your caller, largely invalidating the point of making some kind of comptime routine in the first place. Therefore comptime can be used as a toy, or in C-style language-enforced NIH mode (where you [re]write every single routine you need yourself, or at least know the intimate implementation details of all of them), but not in a serious programming stance.
- csande17 4y agoIf I remember right, FixedBufferAllocator requires you to set an upper bound for the amount of memory you allocate -- you don't have to get the number exactly right or anything. Runtime allocators also have a limit to how much memory you can allocate. It's usually higher, but there's still a limit. Your computer doesn't have an infinite amount of memory.
- anonymoushn 4y ago
- jerf 4y ago"Zig's use of comptime is fairly revolutionary in a c-like language." I might phrase that as "statically-typed language". Most (if not all) of the dynamically-typed languages are technically programs that run at the only "compile time" there is and can do arbitrarily whacky things as a result. It is generally a good idea not to think of them that way. I've removed quite a few top-level Perl statements in my time and enforced rules that a module really should be nothing but declarations of constants and subroutines unless there's an absolute emergency. But dynamically typed languages will generally let you read some files off a disk, query a database schema, and hit an HTTP API to build a "module" if you want to. And in this case the entire language really is available. The limitations tend to come with the increasingly fragile order of operations the modules impose on each other, rather than technical capability. I don't say this to slag on Zig. Increasing the range of what static languages can do is a legitimately interesting topic. This is to shed light on the general space of programming langauges.
- p0nce 4y agoIt just copied D which did it more than 10 years before Zig existed...
- avgcorrection 4y ago> I might phrase that as "statically-typed language". It it is not revolutionary in the context of such languages. Out of the mainstream but not revolutionary.
- dleslie 4y agoJust an FYI, in 2008 PLT Scheme had a language with static typing and macros capable of comptime-like behavior, called Typed Scheme. It lives on today in Racket. But yeah, dynamically typed languages have been doing this for decades, particularly prevalent in Perl circles.
- jdougan 4y agoThe Java Syntactic Extender, which I think was 2001, should be interesting to you then. https://people.csail.mit.edu/jrb/papers/jse-oopsla-2001.pdf https://people.csail.mit.edu/jrb/papers/jse-oopsla-2001.pdf
- zem 4y agohere it is in D: https://tour.dlang.org/tour/en/gems/compile-time-function-evaluation-ctfe https://tour.dlang.org/tour/en/gems/compile-time-function-ev...
- titzer 4y agoVirgil [1] has had compile-time initialization since 2006. There is no restriction on the code that can be run at compile-time. It needs no specific designation--it is simply the initializers of components (globals) and their variables. The entire reachable heap is optimized together with the program to produce a binary. [1] https://dl.acm.org/doi/10.1145/1167515.1167489 https://dl.acm.org/doi/10.1145/1167515.1167489
- WalterBright 4y ago> Zig's use of comptime is fairly revolutionary in a c-like language. The D programming language popularized it in 2006 or 2007. Here's it in action: int square(int x) { return x * x; } int[square(3)] a; // declare array `a` of 9 ints The embedded C compiler in D, ImportC, also can do CTFE: int square(int x) { return x * x; } _Static_assert(square(3) == 9, "Error, Will Robinson!"); You'll see a lot of that in the test suite for ImportC, because the test suite runs a lot faster if it doesn't need to link and load.
- WalterBright 4y agoHere's a more complex example. Suppose we want to initialize an array of squares: int[4] squares1 = [0, 1, 4, 9]; // doing it by hand int[4] squares2 = () { int[4] a; foreach (i; 0 .. 4) a[i] = square(i); return a; } (); // doing it with CTFE squares2 declares a lambda, which returns the initialized array, and then executes the lambda.
- childintime 4y agoD sure looks like the language everybody "C++" actually wanted, if they only knew they wanted it. I actually include myself in that camp, so thanks for taking the stage :D
- cyber_kinetist 4y agoThe reason D didn’t really gain traction over the years was because it required a garbage collector. Recently it’s going the other direction via betterC (a mode where you can ditch the GC), but many parts of the standard library still require the GC to run properly.
- pjmlp 4y agoC# hasn't had the same issues, https://www.wildernesslabs.co/ https://www.wildernesslabs.co/ https://nikolayk.medium.com/getting-started-with-unity-dots-part-1-ecs-7f963777db8e https://nikolayk.medium.com/getting-started-with-unity-dots-... Had Remedy experiment worked out, maybe DOTS would be using D instead of having their own compiler infrastructure for HPC#. Nor Java even, https://www.ptc.com/en/products/developer-tools/perc https://www.ptc.com/en/products/developer-tools/perc https://www.aicas.com/wp/products-services/jamaicavm/ https://www.aicas.com/wp/products-services/jamaicavm/ The main reasons are not having a big name company that would push D no matter what, and lack of focus where D should aim for.
- boundchecked 4y agoZig's comptime variables had this fairly bonkers behavior that basically evaluate by themselves regardless of the underlying runtime controlflow[1], I thought it was cool, but seems to be disabled on trunk build. [1]https://godbolt.org/z/dbnG9eev7 https://godbolt.org/z/dbnG9eev7
- Mathnerd314 4y agoFrom testing it seems like it is executing both branches, i.e. if (b) { v+= x; } else { v += y; } is effectively v += x + y. That doesn't make much sense so I can see why it was fixed.
- Tozen 4y ago> Zig's use of comptime is fairly revolutionary in a c-like language Not revolutionary. Before Zig came out, which was 2016, there was Jonathan Blow talking about and arguably repopularizing it. Lots of people (who knows how many developers) and languages were influenced by him too. 2014, Jonathan Blow, who made lots of videos and gave lots of talks, before going on to officially promote Jai (2016). And it appears he was playing around with prototypes much earlier and talking about it (from 2014). https://youtu.be/UTqZNujQOlA https://youtu.be/UTqZNujQOlA (Demo: Base language, compile-time execution)
- pverghese 4y agoNot very useful to us when Jai has not been released to the public. I would like it to, but currently Zig is more complete than Jai, atleast with respect to it being able to be used by everyone
- Tozen 4y agoBy the time Zig reaches 1.0, Jai may have too or have been publicly released. And Jai's constraints on getting access to their Beta, appears to have been loosening over time.
- deleted 4y ago[deleted]
- pjmlp 4y agoYou don't need those shenanigans when using the Circle compiler, where the entire language is indeed available at compile time.
- rurban 4y ago> zig's use of comptime is fairly revolutionary in a c-like language. It's not, it's just fairly insecure. comptime is known forever, in static and dynamic languages. Better languages do know about the security and usage implications and do something about it.