268
268
269
269
pub fn main() -> %void {
270
270
// If this program is run without stdout attached, exit with an error.
271
- var stdout_file = %return std.io.getStdOut();
271
+ var stdout_file = try std.io.getStdOut();
272
272
// If this program encounters pipe failure when printing to stdout, exit
273
273
// with an error.
274
- %return stdout_file.write("Hello, world!\n");
274
+ try stdout_file.write("Hello, world!\n");
275
275
}</code></pre>
276
276
<pre><code class="sh">$ zig build-exe hello.zig
277
277
$ ./hello
@@ -3224,14 +3224,14 @@ pub fn parseU64(buf: []const u8, radix: u8) -> %u64 {
3224
3224
// ...
3225
3225
}</code></pre>
3226
3226
<p>
3227
- There is a shortcut for this. The <code>%return </code> expression:
3227
+ There is a shortcut for this. The <code>try </code> expression:
3228
3228
</p>
3229
3229
<pre><code class="zig">fn doAThing(str: []u8) -> %void {
3230
- const number = %return parseU64(str, 10);
3230
+ const number = try parseU64(str, 10);
3231
3231
// ...
3232
3232
}</code></pre>
3233
3233
<p>
3234
- <code>%return </code> evaluates an error union expression. If it is an error, it returns
3234
+ <code>try </code> evaluates an error union expression. If it is an error, it returns
3235
3235
from the current function with the same error. Otherwise, the expression results in
3236
3236
the unwrapped value.
3237
3237
</p>
@@ -3278,7 +3278,7 @@ pub fn parseU64(buf: []const u8, radix: u8) -> %u64 {
3278
3278
Example:
3279
3279
</p>
3280
3280
<pre><code class="zig">fn createFoo(param: i32) -> %Foo {
3281
- const foo = %return tryToAllocateFoo();
3281
+ const foo = try tryToAllocateFoo();
3282
3282
// now we have allocated foo. we need to free it if the function fails.
3283
3283
// but we want to return it if the function succeeds.
3284
3284
%defer deallocateFoo(foo);
@@ -3928,11 +3928,11 @@ pub fn printf(self: &OutStream, comptime format: []const u8, args: ...) ->
3928
3928
switch (state) {
3929
3929
State.Start => switch (c) {
3930
3930
'{' => {
3931
- if (start_index < i) %return self.write(format[start_index...i]);
3931
+ if (start_index < i) try self.write(format[start_index...i]);
3932
3932
state = State.OpenBrace;
3933
3933
},
3934
3934
'}' => {
3935
- if (start_index < i) %return self.write(format[start_index...i]);
3935
+ if (start_index < i) try self.write(format[start_index...i]);
3936
3936
state = State.CloseBrace;
3937
3937
},
3938
3938
else => {},
@@ -3943,7 +3943,7 @@ pub fn printf(self: &OutStream, comptime format: []const u8, args: ...) ->
3943
3943
start_index = i;
3944
3944
},
3945
3945
'}' => {
3946
- %return self.printValue(args[next_arg]);
3946
+ try self.printValue(args[next_arg]);
3947
3947
next_arg += 1;
3948
3948
state = State.Start;
3949
3949
start_index = i + 1;
@@ -3968,9 +3968,9 @@ pub fn printf(self: &OutStream, comptime format: []const u8, args: ...) ->
3968
3968
}
3969
3969
}
3970
3970
if (start_index < format.len) {
3971
- %return self.write(format[start_index...format.len]);
3971
+ try self.write(format[start_index...format.len]);
3972
3972
}
3973
- %return self.flush();
3973
+ try self.flush();
3974
3974
}</code></pre>
3975
3975
<p>
3976
3976
This is a proof of concept implementation; the actual function in the standard library has more
@@ -3984,12 +3984,12 @@ pub fn printf(self: &OutStream, comptime format: []const u8, args: ...) ->
3984
3984
and emits a function that actually looks like this:
3985
3985
</p>
3986
3986
<pre><code class="zig">pub fn printf(self: &OutStream, arg0: i32, arg1: []const u8) -> %void {
3987
- %return self.write("here is a string: '");
3988
- %return self.printValue(arg0);
3989
- %return self.write("' here is a number: ");
3990
- %return self.printValue(arg1);
3991
- %return self.write("\n");
3992
- %return self.flush();
3987
+ try self.write("here is a string: '");
3988
+ try self.printValue(arg0);
3989
+ try self.write("' here is a number: ");
3990
+ try self.printValue(arg1);
3991
+ try self.write("\n");
3992
+ try self.flush();
3993
3993
}</code></pre>
3994
3994
<p>
3995
3995
<code>printValue</code> is a function that takes a parameter of any type, and does different things depending
@@ -5891,7 +5891,7 @@ TypeExpr = PrefixOpExpression | "var"
5891
5891
5892
5892
BlockOrExpression = Block | Expression
5893
5893
5894
- Expression = ReturnExpression | BreakExpression | AssignmentExpression
5894
+ Expression = TryExpression | ReturnExpression | BreakExpression | AssignmentExpression
5895
5895
5896
5896
AsmExpression = "asm" option("volatile") "(" String option(AsmOutput) ")"
5897
5897
@@ -5915,7 +5915,7 @@ AssignmentExpression = UnwrapExpression AssignmentOperator UnwrapExpression | Un
5915
5915
5916
5916
AssignmentOperator = "=" | "*=" | "/=" | "%=" | "+=" | "-=" | "<<=" | ">>=" | "&=" | "^=" | "|=" | "*%=" | "+%=" | "-%="
5917
5917
5918
- BlockExpression(body) = Block | IfExpression(body) | TryExpression (body) | TestExpression(body) | WhileExpression(body) | ForExpression(body) | SwitchExpression | CompTimeExpression(body)
5918
+ BlockExpression(body) = Block | IfExpression(body) | IfErrorExpression (body) | TestExpression(body) | WhileExpression(body) | ForExpression(body) | SwitchExpression | CompTimeExpression(body)
5919
5919
5920
5920
CompTimeExpression(body) = "comptime" body
5921
5921
@@ -5929,15 +5929,17 @@ ForExpression(body) = option(Symbol ":") option("inline") "for" "(" Expression "
5929
5929
5930
5930
BoolOrExpression = BoolAndExpression "or" BoolOrExpression | BoolAndExpression
5931
5931
5932
- ReturnExpression = option("%") "return" option(Expression)
5932
+ ReturnExpression = "return" option(Expression)
5933
+
5934
+ TryExpression = "try" Expression
5933
5935
5934
5936
BreakExpression = "break" option(":" Symbol) option(Expression)
5935
5937
5936
5938
Defer(body) = option("%") "defer" body
5937
5939
5938
5940
IfExpression(body) = "if" "(" Expression ")" body option("else" BlockExpression(body))
5939
5941
5940
- TryExpression (body) = "if" "(" Expression ")" option("|" option("*") Symbol "|") body "else" "|" Symbol "|" BlockExpression(body)
5942
+ IfErrorExpression (body) = "if" "(" Expression ")" option("|" option("*") Symbol "|") body "else" "|" Symbol "|" BlockExpression(body)
5941
5943
5942
5944
TestExpression(body) = "if" "(" Expression ")" option("|" option("*") Symbol "|") body option("else" BlockExpression(body))
5943
5945
@@ -5987,7 +5989,7 @@ ContainerInitBody = list(StructLiteralField, ",") | list(Expression, ",")
5987
5989
5988
5990
StructLiteralField = "." Symbol "=" Expression
5989
5991
5990
- PrefixOp = "!" | "-" | "~" | "*" | ("&" option("align" "(" Expression option(":" Integer ":" Integer) ")" ) option("const") option("volatile")) | "?" | "%" | "%%" | "??" | "-%"
5992
+ PrefixOp = "!" | "-" | "~" | "*" | ("&" option("align" "(" Expression option(":" Integer ":" Integer) ")" ) option("const") option("volatile")) | "?" | "%" | "%%" | "??" | "-%" | "try"
5991
5993
5992
5994
PrimaryExpression = Integer | Float | String | CharLiteral | KeywordLiteral | GroupedExpression | BlockExpression(BlockOrExpression) | Symbol | ("@" Symbol FnCallExpression) | ArrayType | FnProto | AsmExpression | ("error" "." Symbol) | ContainerDecl | ("continue" option(":" Symbol))
5993
5995
0 commit comments