authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-05-24 20:59:19-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-05-24 20:59:19-04:00
logfa5b0ef54fe7a612754c796a899020c73d5fb191
tree764825011184f05994c43e467abcd06b361132d0
parent938d791b23d58abf2bad4aa90ae3496ae9bb2435

doc fixups


1 files changed, 42 insertions(+), 30 deletions(-)

doc/langref.html.in+42-30
......@@ -3111,40 +3111,48 @@ test "error union" {
31113111 {#code_end#}
31123112 <p>TODO the <code>||</code> operator for error sets</p>
31133113 {#header_open|Inferred Error Sets#}
3114{#code_begin|syntax#}
3115// Defining error set
3116const 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.
3124fn 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;
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
3120pub 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;
31283123}
31293124
3130// You could also just infer the error set from the given thrown errors
3131fn 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;
3125// With an explicit error set
3126pub 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;
31363129}
31373130
3138// Quick note: inferAdd creates a definition that has a return type that is;
3139const InferAddErrorSet = error {
3140 Zero,
3141 Negative,
3131const Error = error {
3132 Overflow,
31423133};
3143// Which since it contains only errors from NumberError it can be passed to functions like;
3144fn 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.
3134
3135const std = @import("std");
3136
3137test "inferred error set" {
3138 if (add_inferred(u8, 255, 1)) |_| unreachable else |err| switch (err) {
3139 error.Overflow => {}, // ok
3140 }
3141}
31473142{#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>
31483156 {#header_close#}
31493157 {#header_close#}
31503158 {#header_open|Error Return Traces#}
......@@ -3913,10 +3921,14 @@ pub fn main() void {
39133921 {#header_open|@ArgType#}
39143922 <pre><code class="zig">@ArgType(comptime T: type, comptime n: usize) -&gt; type</code></pre>
39153923 <p>
3916 This builtin function takes a function type and returns the type of the 'n'th parameter.
3924 This builtin function takes a function type and returns the type of the parameter at index <code>n</code>.
39173925 </p>
39183926 <p>
3919 <code>T</code> must be a function type, and <code>n</code> must be an <code>usize</code> integer.
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>
39203932 {#header_close#}
39213933 {#header_open|@atomicLoad#}
39223934 <pre><code class="zig">@atomicLoad(comptime T: type, ptr: &amp;const T, comptime ordering: builtin.AtomicOrder) -&gt; T</code></pre>