Why doesn't Zig allow expressions as function bodies

Why isn’t the syntax

fn x(a: u32, b: u32) u32 a + b;

allowed? It would match with the rest of the syntax (if, switch, for, while, etc).

It isn’t necessary, and for all we know it could require additional complexity in the compiler for no compelling reason

IIRC there is a rejected proposal about expression functions.

1 Like

Two identifiers with a space between seems fishy…

1 Like

Usually in Zig the question is the other way around: why would we add it?

That way if it can’t be clearly proven that the language would be better afterwards, it doesn’t get added, which is one of the reasons why Zig is still a small easy to learn language.


I think the moment you would add function expressions, people would want a whole bunch of functional programming features added, which are just alternative ways to writing imperative programs, so Zig’s “Only one obvious way to do things.” would completely fall apart, because now everything would need to be done in an imperative and functional style.

At least that is some of my own reasoning I have come up with, as to why function expressions aren’t added. Also I think functional programming isn’t very good at “Focus[ing] on code rather than style.”.

I don’t have anything against functional programming in general, I just don’t think it is a good fit for what Zig’s core language is going for.

Personally I quite like multi-paradigm languages, but I also like Zig’s clarity and focused direction.

17 Likes