Having your compile-time cake and eating it too

22 points by emschwartz


andrewrk

Programmers should have to explicitly run things at compile-time. The language shouldn’t do it for them.

Most programming languages disagree with this, including C:

char array[3 + 4]; // OK

It only worked because the expression was compile-time known. If we use a runtime-known value, it fails:

int len;
char array[len]; // error: variable length array declaration not allowed at file scope

Here the language ran 3 + 4 at compile-time. So why doesn’t this work?

static unsigned len() { return 3 + 4; }
char array[len()]; // error: variable length array declaration not allowed at file scope

This is silly. Every language can and should support this. No special keyword needed.

jado

To understand what type weird_function returns, you’ll need to understand exactly how ListIfOddLength works. You’d have to know what % means and what to_string does. … With silly type-to-type functions like this, you lose the ability to reason about your program.

I disagree that this example even showcases an increased difficulty in reasoning about these kinds of functions. Since the type-level operators are the same as the value-level operators, I can parse the ListIfOddLength function, however weird it is, faster than any spelling of the same idea in TypeScript/Rust/Scala/etc.

If anything, those HM++ type systems make it harder to reason about more complicated types, requiring new syntax or macros for the same concepts. Zig closes that gap, letting me use my existing taxonomy at the type-level.

Yes, you heard it here first: types are not values. Especially if you want the compiler to do logic and proofs with them, like Hindley-Milner requires. Zig tried having types be values, and it led to stuff like weird_function where you don’t know what type things are until you compile your program, which defeats the whole purpose.

Reports of Zig’s incongruence with IDEs are greatly exaggerated. ZLS has existed for years, letting you see the types of things “before you compile”. I will be continuing to think that types are values.