| ... | @@ -3111,7 +3111,48 @@ test "error union" { | ... | @@ -3111,7 +3111,48 @@ test "error union" { |
| 3111 | {#code_end#} | 3111 | {#code_end#} |
| 3112 | <p>TODO the <code>||</code> operator for error sets</p> | 3112 | <p>TODO the <code>||</code> operator for error sets</p> |
| 3113 | {#header_open|Inferred Error Sets#} | 3113 | {#header_open|Inferred Error Sets#} |
| 3114 | <p>TODO</p> | 3114 | <p> |
| | 3115 | Because many functions in Zig return a possible error, Zig supports inferring the error set. |
| | 3116 | To infer the error set for a function, use this syntax: |
| | 3117 | </p> |
| | 3118 | {#code_begin|test#} |
| | 3119 | // With an inferred error set |
| | 3120 | pub fn add_inferred(comptime T: type, a: T, b: T) !T { |
| | 3121 | var answer: T = undefined; |
| | 3122 | return if (@addWithOverflow(T, a, b, &answer)) error.Overflow else answer; |
| | 3123 | } |
| | 3124 | |
| | 3125 | // With an explicit error set |
| | 3126 | pub fn add_explicit(comptime T: type, a: T, b: T) Error!T { |
| | 3127 | var answer: T = undefined; |
| | 3128 | return if (@addWithOverflow(T, a, b, &answer)) error.Overflow else answer; |
| | 3129 | } |
| | 3130 | |
| | 3131 | const Error = error { |
| | 3132 | Overflow, |
| | 3133 | }; |
| | 3134 | |
| | 3135 | const std = @import("std"); |
| | 3136 | |
| | 3137 | test "inferred error set" { |
| | 3138 | if (add_inferred(u8, 255, 1)) |_| unreachable else |err| switch (err) { |
| | 3139 | error.Overflow => {}, // ok |
| | 3140 | } |
| | 3141 | } |
| | 3142 | {#code_end#} |
| | 3143 | <p> |
| | 3144 | When a function has an inferred error set, that function becomes generic and thus it becomes |
| | 3145 | trickier to do certain things with it, such as obtain a function pointer, or have an error |
| | 3146 | set that is consistent across different build targets. Additionally, inferred error sets |
| | 3147 | are incompatible with recursion. |
| | 3148 | </p> |
| | 3149 | <p> |
| | 3150 | In these situations, it is recommended to use an explicit error set. You can generally start |
| | 3151 | with an empty error set and let compile errors guide you toward completing the set. |
| | 3152 | </p> |
| | 3153 | <p> |
| | 3154 | These limitations may be overcome in a future version of Zig. |
| | 3155 | </p> |
| 3115 | {#header_close#} | 3156 | {#header_close#} |
| 3116 | {#header_close#} | 3157 | {#header_close#} |
| 3117 | {#header_open|Error Return Traces#} | 3158 | {#header_open|Error Return Traces#} |
| ... | @@ -3878,7 +3919,16 @@ pub fn main() void { | ... | @@ -3878,7 +3919,16 @@ pub fn main() void { |
| 3878 | </p> | 3919 | </p> |
| 3879 | {#header_close#} | 3920 | {#header_close#} |
| 3880 | {#header_open|@ArgType#} | 3921 | {#header_open|@ArgType#} |
| 3881 | <p>TODO</p> | 3922 | <pre><code class="zig">@ArgType(comptime T: type, comptime n: usize) -&gt; type</code></pre> |
| | 3923 | <p> |
| | 3924 | This builtin function takes a function type and returns the type of the parameter at index <code>n</code>. |
| | 3925 | </p> |
| | 3926 | <p> |
| | 3927 | <code>T</code> must be a function type. |
| | 3928 | </p> |
| | 3929 | <p> |
| | 3930 | Note: This function is deprecated. Use {#link|@typeInfo#} instead. |
| | 3931 | </p> |
| 3882 | {#header_close#} | 3932 | {#header_close#} |
| 3883 | {#header_open|@atomicLoad#} | 3933 | {#header_open|@atomicLoad#} |
| 3884 | <pre><code class="zig">@atomicLoad(comptime T: type, ptr: &amp;const T, comptime ordering: builtin.AtomicOrder) -&gt; T</code></pre> | 3934 | <pre><code class="zig">@atomicLoad(comptime T: type, ptr: &amp;const T, comptime ordering: builtin.AtomicOrder) -&gt; T</code></pre> |