Add documentation on function parameter type inference.

This commit is contained in:
Maximilian Hunt 2019-10-11 09:41:21 +01:00 committed by Andrew Kelley
parent 0dbdb6329f
commit dc080573d1

View File

@ -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#}