commit dc080573d160a444e2e7c1fb88fac27c8060269f (tree) parent 0dbdb6329fd5e2e47703db00e7da7ddc05078e52 Author: Maximilian Hunt <max@huntw3.com> Date: Fri, 11 Oct 2019 09:41:21 +0100 Add documentation on function parameter type inference. Diffstat:
| M | doc/langref.html.in | | | 23 | +++++++++++++++++++++++ |
1 file changed, 23 insertions(+), 0 deletions(-)
diff --git a/doc/langref.html.in b/doc/langref.html.in @@ -3887,6 +3887,29 @@ test "pass struct to function" { 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#} const assert = @import("std").debug.assert;