From dc080573d160a444e2e7c1fb88fac27c8060269f Mon Sep 17 00:00:00 2001 From: Maximilian Hunt Date: Fri, 11 Oct 2019 09:41:21 +0100 Subject: [PATCH] Add documentation on function parameter type inference. --- doc/langref.html.in | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) 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 @@ -3886,6 +3886,29 @@ test "pass struct to function" {

For extern functions, Zig follows the C ABI for passing structs and unions by value.

+ {#header_close#} + {#header_open|Function Parameter Type Inference#} +

+ 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. +

+ {#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#}