| ... | ... | @@ -3111,7 +3111,40 @@ test "error union" { |
| 3111 | 3111 | {#code_end#} |
| 3112 | 3112 | <p>TODO the <code>||</code> operator for error sets</p> |
| 3113 | 3113 | {#header_open|Inferred Error Sets#} |
| 3114 | | <p>TODO</p> |
| 3114 | {#code_begin|syntax#} |
| 3115 | // Defining error set |
| 3116 | const NumberError = error { |
| 3117 | Zero, |
| 3118 | Negative, |
| 3119 | }; |
| 3120 | |
| 3121 | // While you could define it like this explicitly saying the error domain. |
| 3122 | // Which means you can return an error like `error.InvalidX` as it is not |
| 3123 | // within the NumberError error enum. |
| 3124 | fn positiveAdd(a: i32, b: i32) NumberError!i32 { |
| 3125 | if (a == 0 or b == 0) return NumberError.Zero; |
| 3126 | if (a < 0 or b < 0) return NumberError.Negative; |
| 3127 | return a + b; |
| 3128 | } |
| 3129 | |
| 3130 | // You could also just infer the error set from the given thrown errors |
| 3131 | fn inferAdd(a: i32, b: i32) !i32 { |
| 3132 | // Note: you could either do NumberError.Zero here or just error.Zero |
| 3133 | if (a == 0 or b == 0) return error.Zero; |
| 3134 | if (a < 0 or b < 0) return error.Negative; |
| 3135 | return a + b; |
| 3136 | } |
| 3137 | |
| 3138 | // Quick note: inferAdd creates a definition that has a return type that is; |
| 3139 | const InferAddErrorSet = error { |
| 3140 | Zero, |
| 3141 | Negative, |
| 3142 | }; |
| 3143 | // Which since it contains only errors from NumberError it can be passed to functions like; |
| 3144 | fn printNumberError(err: NumberError) void { } |
| 3145 | // However if it also returned an error outside NumberError it would produce a compile error |
| 3146 | // if passed into the above function. |
| 3147 | {#code_end#} |
| 3115 | 3148 | {#header_close#} |
| 3116 | 3149 | {#header_close#} |
| 3117 | 3150 | {#header_open|Error Return Traces#} |
| ... | ... | @@ -3878,7 +3911,12 @@ pub fn main() void { |
| 3878 | 3911 | </p> |
| 3879 | 3912 | {#header_close#} |
| 3880 | 3913 | {#header_open|@ArgType#} |
| 3881 | | <p>TODO</p> |
| 3914 | <pre><code class="zig">@ArgType(comptime T: type, comptime n: usize) -&gt; type</code></pre> |
| 3915 | <p> |
| 3916 | This builtin function takes a function type and returns the type of the 'n'th parameter. |
| 3917 | </p> |
| 3918 | <p> |
| 3919 | <code>T</code> must be a function type, and <code>n</code> must be an <code>usize</code> integer. |
| 3882 | 3920 | {#header_close#} |
| 3883 | 3921 | {#header_open|@atomicLoad#} |
| 3884 | 3922 | <pre><code class="zig">@atomicLoad(comptime T: type, ptr: &amp;const T, comptime ordering: builtin.AtomicOrder) -&gt; T</code></pre> |