Commit dc080573d1
Changed files (1)
doc/langref.html.in
@@ -3886,6 +3886,29 @@ test "pass struct to function" {
<p>
For extern functions, Zig follows the C ABI for passing structs and unions by value.
</p>
+ {#header_close#}
+ {#header_open|Function Parameter Type Inference#}
+ <p>
+ Function parameters can be declared with {#syntax#}var{#endsyntax#} in place of the type.
+ In this case the parameter types will be inferred when the function is called.
+ Use {#link|@typeOf#} and {#link|@typeInfo#} to get information about the inferred type.
+ </p>
+ {#code_begin|test#}
+const assert = @import("std").debug.assert;
+
+fn addFortyTwo(x: var) @typeOf(x) {
+ return x + 42;
+}
+
+test "fn type inference" {
+ assert(addFortyTwo(1) == 43);
+ assert(@typeOf(addFortyTwo(1)) == comptime_int);
+ var y: i64 = 2;
+ assert(addFortyTwo(y) == 44);
+ assert(@typeOf(addFortyTwo(y)) == i64);
+}
+ {#code_end#}
+
{#header_close#}
{#header_open|Function Reflection#}
{#code_begin|test#}