5 ms·
Very cool. But the more I see of compile-time code the more I think it's a bad idea. The presented "toy" function with the inline while loop is essentially a ma
by doomrobo 10y ago
Very cool. But the more I see of compile-time code the more I think it's a bad idea. The presented "toy" function with the inline while loop is essentially a macro that's doing compile-time code generation. I can easily see myself calling that function with a non-constant value and expecting it to compile (after all, functions are functions and they take in values regardless of their token type). Conflating the two runtimes seems like it could be frustrating. In a lot of cases you also get the same benefits (in this case, monomorphization) automatically due to compiler optimizations. Also I think most people working in production environments would have some sort of expectation about the time it takes to compile a library in Zig, but allowing arbitrary compile-time computation means that they cannot even guarantee that the compilation will halt!
- ben-schaaf 10y agoThe templating systems in C++ and D are Turing complete. Apparently Rust's type system is also Turning complete.
- gpderetta 10y agoThe type system of every (statically typed) programming language attempts to expand until it becomes Turing complete. Those languages which cannot so expand are replaced by ones which can. With apologies to jwz.
- pg314 10y agoArbitrary compile time evaluations in the form of macros have been around in Lisp since 1963 [1]. I'm sure that pretty much every Lisp programmer thinks macros are a great idea. If you want to learn more about them, I recommend Practical Common Lisp (a great intro) [2] or On Lisp (requires prior Common Lisp knowledge) [3]. [1] ftp://publications.ai.mit.edu/ai-publications/pdf/AIM-057.pdf [2] http://www.gigamonkeys.com/book/ http://www.gigamonkeys.com/book/ [3] http://www.paulgraham.com/onlisptext.html http://www.paulgraham.com/onlisptext.html
- mapcars 10y agoIt's so strange to see people discussing cool new ideas, which are half-century old. Seems like we need history lessons for programmers specifically.
- jonathanstrange 10y agoI think the right model for compile-time evaluation is to have an interpreter and phases, making the full language available at compile-time but clearly distinguishing between compile-time, macro-expansion, module elaboration and runtime phases. Scheme dialects generally do that, although they should maybe define module visiting and elaboration orders in general more clearly like e.g. Ada does. I don't know if Zig does that. Anyway, having different phases is not conceptually hard, although it can bite you a bit with Macros in Scheme.
- billsix 10y agoIn my project, http://billsix.github.io/cac.html http://billsix.github.io/cac.html, the Compile-Time language is the same as the runtime language. Tests are collocated with procedure definitions, but evaluate only at Compile-Time.