authorgravatar for max@huntw3.comMaximilian Hunt <max@huntw3.com> 2019-10-11 09:41:21+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-10-22 15:28:21-04:00
logdc080573d160a444e2e7c1fb88fac27c8060269f
tree116ab3f3849e86ddb619a1d0e68ecd43dea11132
parent0dbdb6329fd5e2e47703db00e7da7ddc05078e52

Add documentation on function parameter type inference.


1 files changed, 23 insertions(+), 0 deletions(-)

doc/langref.html.in+23
...@@ -3886,6 +3886,29 @@ test "pass struct to function" {...@@ -3886,6 +3886,29 @@ test "pass struct to function" {
3886 <p>3886 <p>
3887 For extern functions, Zig follows the C ABI for passing structs and unions by value.3887 For extern functions, Zig follows the C ABI for passing structs and unions by value.
3888 </p>3888 </p>
3889 {#header_close#}
3890 {#header_open|Function Parameter Type Inference#}
3891 <p>
3892 Function parameters can be declared with {#syntax#}var{#endsyntax#} in place of the type.
3893 In this case the parameter types will be inferred when the function is called.
3894 Use {#link|@typeOf#} and {#link|@typeInfo#} to get information about the inferred type.
3895 </p>
3896 {#code_begin|test#}
3897const assert = @import("std").debug.assert;
3898
3899fn addFortyTwo(x: var) @typeOf(x) {
3900 return x + 42;
3901}
3902
3903test "fn type inference" {
3904 assert(addFortyTwo(1) == 43);
3905 assert(@typeOf(addFortyTwo(1)) == comptime_int);
3906 var y: i64 = 2;
3907 assert(addFortyTwo(y) == 44);
3908 assert(@typeOf(addFortyTwo(y)) == i64);
3909}
3910 {#code_end#}
3911
3889 {#header_close#}3912 {#header_close#}
3890 {#header_open|Function Reflection#}3913 {#header_open|Function Reflection#}
3891 {#code_begin|test#}3914 {#code_begin|test#}