News for hackers. Stuff that matters.
Re:Ziglings: Learn the Zig programming language by fixing ti... by ymbeld on Sunday February 14, 2021 @04:51AM (#26130941)
It doesn’t seem as general as MetaOcaml. Just two-stage evaluation, and without quote and splice.
Re:Ziglings: Learn the Zig programming language by fixing ti... by pron on Sunday February 14, 2021 @07:03AM (#26131659)
I think Zig's comptime has three interesting features:

1. It is referentially transparent, i.e., unlike Lisp, i.e. nothing in the language can distinguish between `1 + 2` and `3`. This means that the semantics of the language is the same as if everything was evaluated at runtime, and the comptime annotation has no impact on semantics (assuming syntax terms aren't objects and there's no `eval`).

2. It supports type introspection and construction.

3. It doesn't have generics and typeclasses as separate constructs but as applications of comptime.

I think 3 is unique to Zig, but I wonder about 1: is it possible in MetaOCaml, as it is in Lisp, C, C++, Rust and Haskell -- but not in Zig! (or, say, Java) -- to write a unit `m`, such that m(1 + 2) is different from m(3)?

Re:Ziglings: Learn the Zig programming language by fixing ti... by ymbeld on Sunday February 14, 2021 @08:27AM (#26132210)
I don’t know. I think MetaOcaml is strict about inspecting Code terms like Code (1 + 2) and Code 3 (as far as I remember it is not allowed). So no, I don’t think you can distinguish between them.
Re:Ziglings: Learn the Zig programming language by fixing ti... by throwaway17_17 on Sunday February 14, 2021 @01:37PM (#26134841)
I think (again based on examples and docs) that it is actually multistaged. However, this is not apparent because the quote and splice are semi-implicit. The ‘comptime’ keyword is essentially a the quote operator, ie. it produces what is essentially a compile time thunk, lazily capturing the ‘code’ value of the expression defined under the comptime keyword. This thunk is directly analogous to a value of type ‘code a’ in MetaML. Then there is an implicit splice operator in place any time a value defined under comptime is used subsequently. I say it is multistaged because it certainly appears that one can define a comptime variable for some computation and then use that variable in a later comptime definition or expression. So, it looks like a basic data flow analysis to determine dependency is done on the comptime evaluations and those are ordered implicitly. This order would correspond to the more explicit staging syntax of MetaML and the operation semantics that underpin it.