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" {...@@ -3111,40 +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{#code_begin|syntax#}3114 <p>
3115// Defining error set3115 Because many functions in Zig return a possible error, Zig supports inferring the error set.
3116const NumberError = error {3116 To infer the error set for a function, use this syntax:
3117 Zero,3117 </p>
3118 Negative,3118{#code_begin|test#}
3119};3119// With an inferred error set
31203120pub fn add_inferred(comptime T: type, a: T, b: T) !T {
3121// While you could define it like this explicitly saying the error domain.3121 var answer: T = undefined;
3122// Which means you can return an error like `error.InvalidX` as it is not3122 return if (@addWithOverflow(T, a, b, &answer)) error.Overflow else answer;
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;
3128}3123}
31293124
3130// You could also just infer the error set from the given thrown errors3125// With an explicit error set
3131fn inferAdd(a: i32, b: i32) !i32 {3126pub fn add_explicit(comptime T: type, a: T, b: T) Error!T {
3132 // Note: you could either do NumberError.Zero here or just error.Zero3127 var answer: T = undefined;
3133 if (a == 0 or b == 0) return error.Zero;3128 return if (@addWithOverflow(T, a, b, &answer)) error.Overflow else answer;
3134 if (a < 0 or b < 0) return error.Negative;
3135 return a + b;
3136}3129}
31373130
3138// Quick note: inferAdd creates a definition that has a return type that is;3131const Error = error {
3139const InferAddErrorSet = error {3132 Overflow,
3140 Zero,
3141 Negative,
3142};3133};
3143// Which since it contains only errors from NumberError it can be passed to functions like;3134
3144fn printNumberError(err: NumberError) void { }3135const std = @import("std");
3145// However if it also returned an error outside NumberError it would produce a compile error3136
3146// if passed into the above function.3137test "inferred error set" {
3138 if (add_inferred(u8, 255, 1)) |_| unreachable else |err| switch (err) {
3139 error.Overflow => {}, // ok
3140 }
3141}
3147{#code_end#}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>
3148 {#header_close#}3156 {#header_close#}
3149 {#header_close#}3157 {#header_close#}
3150 {#header_open|Error Return Traces#}3158 {#header_open|Error Return Traces#}
...@@ -3913,10 +3921,14 @@ pub fn main() void {...@@ -3913,10 +3921,14 @@ pub fn main() void {
3913 {#header_open|@ArgType#}3921 {#header_open|@ArgType#}
3914 <pre><code class="zig">@ArgType(comptime T: type, comptime n: usize) -&gt; type</code></pre>3922 <pre><code class="zig">@ArgType(comptime T: type, comptime n: usize) -&gt; type</code></pre>
3915 <p>3923 <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>.
3917 </p>3925 </p>
3918 <p>3926 <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>
3920 {#header_close#}3932 {#header_close#}
3921 {#header_open|@atomicLoad#}3933 {#header_open|@atomicLoad#}
3922 <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>