13 ms·
Things Zig comptime won't do
- nexo-v1 1y ago[dead]
- pyrolistical 1y agoWhat makes comptime really interesting is how fluid it is as you work. At some point you realize you need type information, so you just add it to your func params. That bubbles all the way up and you are done. Or you realize in certain situation it is not possible to provide the type and you need to solve a arch/design issue.
- Zambyte 1y agoIf the type that you're passing as an argument is the type of another argument, you can keep the API simpler by just using @TypeOf(arg) internally in the function instead.
- gitroom 1y agoCool!
- no_wizard 1y agoI like the Zig language and tooling. I do wish there was a safety mode that give the same guarantees as Rust, but it’s a huge step above C/C++. I am also extremely impressed with the Zig compiler. Perhaps the safety is the tradeoff with the comparative ease of using the language compared to Rust, but I’d love the best of both worlds if it were possible
- hermanradtke 1y agoI wish for “strict” mode as well. My current thinking: TypeScript is to JavaScript as Zig is to C I am a huge TS fan.
- rc00 1y agoIs Zig aiming to extend C or extinguish it? The embrace story is well-established at this point but the remainder is often unclear in the messaging from the community.
- dooglius 1y agoZig is open source, so the analogy to Microsoft's EEE [0] seems misplaced. [0] https://en.m.wikipedia.org/wiki/Embrace,_extend,_and_extinguish?useskin=vector https://en.m.wikipedia.org/wiki/Embrace,_extend,_and_extingu...
- rc00 1y agoOpen source or not isn't the point. The point is the mission and the ecosystem. Some of the Zig proponents laud the C compatibility. Others are seeking out the "pure Zig" ecosystem. Curious onlookers want to know if the Zig ecosystem and community will be as hostile to the decades of C libraries as the Rust zealots have been. To be fair, I don't believe there is a centralized and stated mission with Zig but it does feel like the story has moved beyond the "Incrementally improve your C/C++/Zig codebase" moniker.
- Zambyte 1y ago> Curious onlookers want to know if the Zig ecosystem and community will be as hostile to the decades of C libraries as the Rust zealots have been. Definitely not the case in Zig. From my experience, the relationship with C libraries amounts to "if it works, use it".
- rc00 1y agoAre you referring to static linking? Dynamic linking? Importing/inclusion? How does this translate (no pun intended) when the LLVM backend work is completed? Does this extend to reproducible builds? Hermetic builds? And the relationship with C libraries certainly feels like a placeholder, akin to before the compiler was self-hosted. While I have seen some novel projects in Zig, there are certainly more than a few "pure Zig" rewrites of C libraries. Ultimately, this is free will. I just wonder if the Zig community is teeing up for a repeat of Rust's actix-web drama but rather than being because of the use of unsafe, it would be due to the use of C libraries instead of the all-Zig counterparts (assuming some level of maturity with the latter). While Zig's community appears healthier and more pragmatic, hype and ego have a way of ruining everything.
- xedrac 1y agoI like Zig as a replacement for C, but not C++ due to its lack of RAII. Rust on the other hand is a great replacement for C++. I see Zig as filling a small niche where allocation failures are paramount - very constrained embedded devices, etc... Otherwise, I think you just get a lot more with Rust.
- xmorse 1y agoEven better than RAII would be linear types, but it would require a borrow checker to track the lifetimes of objects. Then you would get a compiler error if you forget to call a .destroy() method
- throwawaymaths 1y agono you just need analysis with a dependent type system (which linear types are a subset of). it doesn't have to be in the compiler. there was a proof of concept here a few months ago: https://news.ycombinator.com/item?id=42923829 https://news.ycombinator.com/item?id=42923829 https://news.ycombinator.com/item?id=43199265 https://news.ycombinator.com/item?id=43199265
- rastignack 1y agoCompile times and painful to refactor codebase are rust’s main drawbacks for me though. It’s totally subjective but I find the language boring to use. For side projects I like having fun thus I picked zig. To each his own of course.
- nicce 1y ago> refactor codebase are rust’s main drawbacks Hard disagree about refactoring. Rust is one of the few languages where you can actually do refactoring rather safely without having tons of tests that just exist to catch issues if code changes.
- rastignack 1y agoLifetimes and generic tend to leak so you have to modify your code all around the place when you touch them though.
- ksec 1y ago>but I’d love the best of both worlds if it were possible I am just going to quote what pcwalton said the other day that perhaps answer your question. >> I’d be much more excited about that promise [memory safety in Rust] if the compiler provided that safety, rather than asking the programmer to do an extraordinary amount of extra work to conform to syntactically enforced safety rules. Put the complexity in the compiler, dudes. > That exists; it's called garbage collection. >If you don't want the performance characteristics of garbage collection, something has to give. Either you sacrifice memory safety or you accept a more restrictive paradigm than GC'd languages give you. For some reason, programming language enthusiasts think that if you think really hard, every issue has some solution out there without any drawbacks at all just waiting to be found. But in fact, creating a system that has zero runtime overhead and unlimited aliasing with a mutable heap is as impossible as finding two even numbers whose sum is odd. [1] https://news.ycombinator.com/item?id=43726315 https://news.ycombinator.com/item?id=43726315
- skybrian 1y agoYes, but I’m not hoping for that. I’m hoping for something like a scripting language with simpler lifetime annotations. Is Rust going to be the last popular language to be invented that explores that space? I hope not.
- hyperbrainer 1y agoI was quite impressed with Austral[0], which used Linear Types and avoids the whole Rust-like implementation in favour of a more easily understandable system, albeit slightly more verbose. [0]https://borretti.me/article/introducing-austral https://borretti.me/article/introducing-austral
- renox 1y agoAustra's concept are interesting but the introduction doesn't show how to handle correctly errors in this language..
- 1y ago
- throwawaymaths 1y agoin principle it should be doable, possibly not in the language/compiler itself, there was this POC a few months ago: https://github.com/ityonemo/clr https://github.com/ityonemo/clr
- pjmlp 1y agoMost of Zig's safety was already available in 1978's Modula-2, but apparently languages have to come in curly brackets for adoption.
- chongli 1y agolanguages have to come in curly brackets for adoption Python and Ruby are two very popular counterexamples.
- pjmlp 1y agoNot really, Ruby has plenty of curly brackets, e.g. 5.times { puts "hello!" }. In both cases, while it wasn't curly brackets that drove their adoption, it was unavoidable frameworks. Most people only use Ruby when they have Rails projects, and what made Python originally interesting was Zope CMS. And nowadays AI/ML frameworks, that are actually written in C, C++ and Fortran, making Python relevant because scientists decided on picking Python for their library bindings, it could have been Tcl just as well, as choices go. So yeah, maybe not always curly brackets, but definitly something that makes it unavoidable, sadly Modula-2 lacked that, an OS vendor pushing it no matter what, FAANG style.
- deleted 1y ago[deleted]
- ashvardanian 1y agoPrevious submission: https://news.ycombinator.com/item?id=43738703 https://news.ycombinator.com/item?id=43738703
- curtisszmania 1y ago[dead]
- karmakaze 1y ago> Zig’s comptime feature is most famous for what it can do: generics!, conditional compilation!, subtyping!, serialization!, ORM! That’s fascinating, but, to be fair, there’s a bunch of languages with quite powerful compile time evaluation capabilities that can do equivalent things. I'm curious what are these other languages that can do these things? I read HN regularly but don't recall them. Or maybe that's including things like Java's annotation processing which is so clunky that I wouldn't classify them to be equivalent.
- awestroke 1y agoRust, D, Nim, Crystal, Julia
- deleted 1y ago[deleted]
- elcritch 1y agoDefinitely, you can do most of those things in Nim without macros using templates and compile time stuff. It’s preferable to macros when possible. Julia has fantastic compile time abilities as well. It’s beautiful to implement an incredibly fast serde in like 10 lines without requiring other devs to annotate their packages. I wouldn’t include Rust on that list if we’re speaking of compile time and compile time type abilities. Last time I tried it Rust’s const expression system is pretty limited. Rust’s macro system likewise is also very weak. Primarily you can only get type info by directly passing the type definition to a macro, which is how derive and all work.
- tialaramex 1y agoRust has two macro systems, the proc macros are allowed to do absolutely whatever they please because they're actually executing in the compiler. Now, should they do anything they please? Definitely not, but they can. That's why there's a (serious) macro which runs your Python code, and a (joke, in the sense that you should never use it, not that it wouldn't work) macro which replaces your running compiler with a different one so that code which is otherwise invalid will compile anyway...
- hiccuphippo 1y agoThe quote in Spanish about a Norse god is from a story by Jorge Luis Borges, here's an English translation: https://biblioklept.org/2019/04/02/the-disk-a-very-short-story-by-jorge-luis-borges/ https://biblioklept.org/2019/04/02/the-disk-a-very-short-sto...
- _emacsomancer_ 1y agoAnd in Spanish here: https://www.poeticous.com/borges/el-disco?locale=es https://www.poeticous.com/borges/el-disco?locale=es (Not having much Spanish, I at first thought "Odin's disco(teque)" and then "no, that doesn't make sense about sides", but then, surely primed by English "disco", thought "it must mean Odin's record/lp/album".)
- wiml 1y agoOdin's records have no B-sides, because everything Odin writes is fire!
- tialaramex 1y agoBack when things really had A and B sides, it was moderately common for big artists to release a "Double A" in which both titles were heavily promoted, e.g. Nirvana's "All Apologies" and "Rape Me" are a double A, the Beatles "Penny Lane" and "Strawberry Fields Forever" likewise.
- kruuuder 1y agoIf you have read the story and, like me, are still wondering which part of the story is the quote at the top of the post: "It's Odin's Disc. It has only one side. Nothing else on Earth has only one side."
- tines 1y agoA mobius strip does!
- ww520 1y agoThis is a very educational blog post. I knew ‘comptime for’ and ‘inline for’ were comptime related, but didn’t know the difference. The post explains the inline version only knows the length at comptime. I guess it’s for loop unrolling.
- hansvm 1y agoThe normal use case for `inline for` is when you have to close over something only known at compile time (like when iterating over the fields of a struct), but when your behavior depends on runtime information (like conditionally assigning data to those fields). Unrolling as a performance optimization is usually slightly different, typically working in batches rather than unrolling the entire thing, even when the length is known at compile time. The docs suggest not using `inline` for performance without evidence it helps in your specific usage, largely because the bloated binary is likely to be slower unless you have a good reason to believe your case is special, and also because `inline` _removes_ optimization potential from the compiler rather than adding it (its inlining passes are very, very good, and despite having an extremely good grasp on which things should be inlined I rarely outperform the compiler -- I'm never worse, but the ability to not have to even think about it unless/until I get to the microoptimization phase of a project is liberating).
- pron 1y agoYes! To me, the uniqueness of Zig's comptime is a combination of two things: 1. comtpime replaces many other features that would be specialised in other languages with or without rich compile-time (or runtime) metaprogramming, and 2. comptime is referentially transparent [1], that makes it strictly "weaker" than AST macros, but simpler to understand; what's surprising is just how capable you can be with a comptime mechanism with access to introspection yet without the referentially opaque power of macros. These two give Zig a unique combination of simplicity and power. We're used to seeing things like that in Scheme and other Lisps, but the approach in Zig is very different. The outcome isn't as general as in Lisp, but it's powerful enough while keeping code easier to understand. You can like it or not, but it is very interesting and very novel (the novelty isn't in the feature itself, but in the place it has in the language). Languages with a novel design and approach that you can learn in a couple of days are quite rare. [1]: In short, this means that you get no access to names or expressions, only the values they yield.
- User23 1y agoHas anyone grafted Zig style macros into Common Lisp?
- toxik 1y agoIsn’t this kind of thing sort of the default thing in Lisp? Code is data so you can transform it.
- fn-mote 1y agoThere are no limitations on the transformations in lisp. That can make macros very hard to understand. And hard for later program transformers to deal with. The innovation in Zig is the restrictions that limit the power of macros.
- TinkersW 1y agoLisp is so powerful, but without static types you can't even do basic stuff like overloading, and have to invent a way to even check the type(for custom types) so you can branch on type.
- ephaeton 1y agozig's comptime has some (objectively: debatable? subjectively: definite) shortcomings that the zig community then overcomes with zig build to generate code-as-strings to be lateron @imported and compiled. Practically, "zig build"-time-eval. As such there's another 'comptime' stage with more freedom, unlimited run-time (no @setEvalBranchQuota), can do IO (DB schema, network lookups, etc.) but you lose the freedom to generate zig types as values in the current compilation; instead of that you of course have the freedom to reduce->project from target compiled semantic back to input syntax down to string to enter your future compilation context again. Back in the day, where I had to glue perl and tcl via C at one point in time, passing strings for perl generated through tcl is what this whole thing reminds me of. Sure it works. I'm not happy about it. There's _another_ "macro" stage that you can't even see in your code (it's just @import). The zig community bewilders me at times with their love for lashing themselves. The sort of discussions which new sort of self-harm they'd love to enforce on everybody is borderline disturbing.
- User23 1y agoLearning XS (maybe with Swig?) was a great way to actually understand Perl.
- bsder 1y ago> The zig community bewilders me at times with their love for lashing themselves. The sort of discussions which new sort of self-harm they'd love to enforce on everybody is borderline disturbing. Personally, I find the idea that a compiler might be able to reach outside itself completely terrifying (Access the network or a database? Are you nuts?). That should be 100% the job of a build system. Now, you can certainly argue that generating a text file may or may not be the best way to reify the result back into the compiler. However, what the compiler gets and generates should be completely deterministic.
- bmacho 1y agoThey are not advocating for IO in the compiler, but everything else that other languages can do with macros: run commands comptime, generate code, read code, modify code. It's proven to be very useful.
- paldepind2 1y agoThis is honestly really cool! I've heard praises about Zig's comptime without really understanding what makes it tick. It initially sounds like Rust's constant evaluation which is not particularly capable. The ability to have types represented as values at compilation time, and _only_ at compile time, is clearly very powerful. It approximates dynamic languages or run-time reflection without any of the run-time overhead and without opening the Pandora's box that is full blown macros as in Lisp or Rust's procedural macros.
- forrestthewoods 1y ago> When you execute code at compile time, on which machine does it execute? The natural answer is “on your machine”, but it is wrong! I don’t understand this. If I am cross-compiling a program is it not true that comptime code literally executes on my local host machine? Like, isn’t that literally the definition of “compile-time”? If there is an endian architecture change I could see Zig choosing to emulate the target machine on the host machine. This feels so wrong to me. HostPlatform and TargetPlatform can be different. That’s fine! Hiding the host platform seems wrong. Can aomeone explain why you want to hide this seemingly critical fact? Don’t get me wrong, I’m 100% on board the cross-compile train. And Zig does it literally better than any other compiled language that I know. So what am I missing? Or wait. I guess the key is that, unlike Jai, comptime Zig code does NOT run at compile time. It merely refers to things that are KNOWN at compile time? Wait that’s not right either. I’m confused.
- int_19h 1y agoThe point is that something like sizeof(pointer) should have the same value in comptime code that it has at runtime for a given app. Which, yes, means that the comptime interpreter emulates the target machine. The reason is fairly simple: you want comptime code to be able to compute correct values for use at runtime. At the same time, there's zero benefit to not hiding the host platform in comptime, because, well, what use case is there for knowing e.g. the size of pointer in the arch on which the compiler is running?
- forrestthewoods 1y ago> Which, yes, means that the comptime interpreter emulates the target machine. Reasonable if that’s how it works. I had absolutely no idea that Zig comptime worked this way! > there's zero benefit to not hiding the host platform in comptime I don’t think this is clear. It is possibly good to hide host platform given Zig’s more limited comptime capabilities. However in my $DayJob an extremely common and painful source of issues is trying to hide host platform when it can not in fact be hidden.
- 1y ago
- mootptr 1y ago[dead]
- riskybabo25479 1y ago[dead]
- bunderbunder 1y agoZig has a completely different feature, partial evaluation/specialization, which, none the less, is enough to cover most of use-cases for dynamic code generation. These kinds of insights are what I love about Zig. Andrew Kelley just might be the patron saint of the KISS principle. A long time ago I had an enlightenment experience where I was doing something clever with macros in F#, and it wasn't until I had more-or-less finished the whole thing that I realized I could implement it in a lot less (and more readable) code by doing some really basic stuff with partial application and higher order functions. And it would still be performant because the compiler would take care of the clever bits for me. Not too long after that, macros largely disappeared from my Lisp code, too.
- minetest2048 1y agoFortunately its not just you, in Julia community there's a thread that discusses why you shouldn't use metaprogramming as a first solution as multiple dispatch and higher order functions are cleaner and faster: https://discourse.julialang.org/t/how-to-warn-new-users-away-from-metaprogramming/35022 https://discourse.julialang.org/t/how-to-warn-new-users-away...