Commit dc080573d1

Maximilian Hunt <max@huntw3.com>
2019-10-11 10:41:21
Add documentation on function parameter type inference.
1 parent 0dbdb63
Changed files (1)
doc/langref.html.in
@@ -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#}