There are two different forms of “return type inference” reflected in the proposals @Sze and @LucasSantos91 linked above, and I think they warrant a more detailed explanation/comparison to avoid confusion and determine which one best matches your expectations.
return type inference · Issue #447 · ziglang/zig · GitHub (rejected): infers the return type based on the function body. For example, in the following simple function, the return type could be inferred as u32
, since the compiler knows that the result of a + b
(both u32
s) is a u32
:
fn add(a: u32, b: u32) anytype {
return a + b;
}
The closest analogy to this in Zig today is inferred error sets (!void
, etc.), which do this sort of analysis, but only on the error set and not the “value” part of the return type.
Proposal: `@Result` to match cast builtins inference API · Issue #16313 · ziglang/zig · GitHub (neither accepted nor rejected as of now): infers the return type based on the result type where the function is called. Many builtins in Zig today do this sort of inference, such as @intCast
:
const a: u64 = 123;
const b: u32 = @intCast(a); // u32 is the result type of @intCast here
This would be useful for writing transformation functions in userspace, such as the one mentioned in the proposal description:
// Current:
const foo = std.fmt.parseInt(u32, content, 10);
// If the proposal were accepted:
const bar: u32 = try std.fmt.parseInt(content, 10);
While this may not look that useful in this simple example, result types become much more useful when used in function parameters, struct field initializers, etc., which might be what you are getting at with