From 938d791b23d58abf2bad4aa90ae3496ae9bb2435 Mon Sep 17 00:00:00 2001
From: "braedonww@gmail.com"
Date: Thu, 17 May 2018 10:43:59 +1000
Subject: [PATCH] Added argtype and error inferring info
---
doc/langref.html.in | 42 ++++++++++++++++++++++++++++++++++++++++--
1 file changed, 40 insertions(+), 2 deletions(-)
diff --git a/doc/langref.html.in b/doc/langref.html.in
index d047c7d9f2..6995cbab33 100644
--- a/doc/langref.html.in
+++ b/doc/langref.html.in
@@ -3111,7 +3111,40 @@ test "error union" {
{#code_end#}
TODO the || operator for error sets
{#header_open|Inferred Error Sets#}
- TODO
+{#code_begin|syntax#}
+// Defining error set
+const NumberError = error {
+ Zero,
+ Negative,
+};
+
+// While you could define it like this explicitly saying the error domain.
+// Which means you can return an error like `error.InvalidX` as it is not
+// within the NumberError error enum.
+fn positiveAdd(a: i32, b: i32) NumberError!i32 {
+ if (a == 0 or b == 0) return NumberError.Zero;
+ if (a < 0 or b < 0) return NumberError.Negative;
+ return a + b;
+}
+
+// You could also just infer the error set from the given thrown errors
+fn inferAdd(a: i32, b: i32) !i32 {
+ // Note: you could either do NumberError.Zero here or just error.Zero
+ if (a == 0 or b == 0) return error.Zero;
+ if (a < 0 or b < 0) return error.Negative;
+ return a + b;
+}
+
+// Quick note: inferAdd creates a definition that has a return type that is;
+const InferAddErrorSet = error {
+ Zero,
+ Negative,
+};
+// Which since it contains only errors from NumberError it can be passed to functions like;
+fn printNumberError(err: NumberError) void { }
+// However if it also returned an error outside NumberError it would produce a compile error
+// if passed into the above function.
+{#code_end#}
{#header_close#}
{#header_close#}
{#header_open|Error Return Traces#}
@@ -3878,7 +3911,12 @@ pub fn main() void {
{#header_close#}
{#header_open|@ArgType#}
- TODO
+ @ArgType(comptime T: type, comptime n: usize) -> type
+
+ This builtin function takes a function type and returns the type of the 'n'th parameter.
+
+
+ T must be a function type, and n must be an usize integer.
{#header_close#}
{#header_open|@atomicLoad#}
@atomicLoad(comptime T: type, ptr: &const T, comptime ordering: builtin.AtomicOrder) -> T