authorgravatar for braedonww@gmail.combraedonww@gmail.com <braedonww@gmail.com> 2018-05-17 10:43:59+10:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-05-24 20:20:07-04:00
log938d791b23d58abf2bad4aa90ae3496ae9bb2435
tree9f7baa14d6916ab8e10bfcdbeea891c11ebdd8af
parent54e887ed9e774c6fd68f51d97e2c5c760e48be30

Added argtype and error inferring info


1 files changed, 40 insertions(+), 2 deletions(-)

doc/langref.html.in+40-2
......@@ -3111,7 +3111,40 @@ test "error union" {
31113111 {#code_end#}
31123112 <p>TODO the <code>||</code> operator for error sets</p>
31133113 {#header_open|Inferred Error Sets#}
3114 <p>TODO</p>
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;
3128}
3129
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;
3136}
3137
3138// Quick note: inferAdd creates a definition that has a return type that is;
3139const InferAddErrorSet = error {
3140 Zero,
3141 Negative,
3142};
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.
3147{#code_end#}
31153148 {#header_close#}
31163149 {#header_close#}
31173150 {#header_open|Error Return Traces#}
......@@ -3878,7 +3911,12 @@ pub fn main() void {
38783911 </p>
38793912 {#header_close#}
38803913 {#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.
38823920 {#header_close#}
38833921 {#header_open|@atomicLoad#}
38843922 <pre><code class="zig">@atomicLoad(comptime T: type, ptr: &amp;const T, comptime ordering: builtin.AtomicOrder) -&gt; T</code></pre>