| ... | ... | @@ -3886,6 +3886,29 @@ test "pass struct to function" { |
| 3886 | 3886 | <p> |
| 3887 | 3887 | For extern functions, Zig follows the C ABI for passing structs and unions by value. |
| 3888 | 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#} |
| 3897 | const assert = @import("std").debug.assert; |
| 3898 | |
| 3899 | fn addFortyTwo(x: var) @typeOf(x) { |
| 3900 | return x + 42; |
| 3901 | } |
| 3902 | |
| 3903 | test "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 | 3912 | {#header_close#} |
| 3890 | 3913 | {#header_open|Function Reflection#} |
| 3891 | 3914 | {#code_begin|test#} |