diff options
| author | Maximilian Hunt <max@huntw3.com> | 2019-10-11 09:41:21 +0100 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2019-10-22 15:28:21 -0400 |
| commit | dc080573d160a444e2e7c1fb88fac27c8060269f (patch) | |
| tree | 116ab3f3849e86ddb619a1d0e68ecd43dea11132 /doc | |
| parent | 0dbdb6329fd5e2e47703db00e7da7ddc05078e52 (diff) | |
| download | zig-dc080573d160a444e2e7c1fb88fac27c8060269f.tar.gz zig-dc080573d160a444e2e7c1fb88fac27c8060269f.zip | |
Add documentation on function parameter type inference.
Diffstat (limited to 'doc')
| -rw-r--r-- | doc/langref.html.in | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/doc/langref.html.in b/doc/langref.html.in index 17792d6470..618ea08cdb 100644 --- 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; |
