Is there a reason zig fmt doesn't add a space between `error` and `{` in error set declarations?

If you format your code using zig fmt, this is what you’ll get:

const ComputationError = error{
    IllegalArgument,
};

Is there a reason there’s no whitespace between error and {? This does not happen with struct or enum declarations.

Thank you!

1 Like

I can’t tell you exactly what the rationale for this is, but if I were to take a guess it might have to do with that error is a slightly different syntactic construct from the other.

struct, union, enum and opaque are containers, which means that they can contain both fields, fn/const/var, and test and comptime blocks.

error on the other hand is only able to list error values (with doc comments), and as a construct it has more in common with a struct or array initializer like

[_]Value{
    one,
    two,
    three,
}

, which doesn’t have a space before the brace, than a struct declaration. So it’s possible that that might have been the rationale.

There’s also an open issue pointing out this inconsistency. Given that T{} and [n]T{} struct/array initializers might be going away in favor of using .{} with a result type, it might provide an opportunity for reconsidering what the canonical formatting for error should be.

7 Likes

Thank you very much for the clear and in depth response. This is really helpful actually and I’ll keep an eye out for that open issue you mentioned.