Skip to content

Commit

Permalink
replace a %% b with a catch b
Browse files Browse the repository at this point in the history
See #632

better fits the convention of using keywords for control flow
  • Loading branch information
andrewrk committed Jan 7, 2018
1 parent 66717db commit 632d143
Show file tree
Hide file tree
Showing 32 changed files with 173 additions and 170 deletions.
2 changes: 1 addition & 1 deletion doc/docgen.zig
Expand Up @@ -45,7 +45,7 @@ const State = enum {
fn gen(in: &io.InStream, out: &io.OutStream) {
var state = State.Start;
while (true) {
const byte = in.readByte() %% |err| {
const byte = in.readByte() catch |err| {
if (err == error.EndOfStream) {
return;
}
Expand Down
12 changes: 6 additions & 6 deletions doc/home.html.in
Expand Up @@ -142,16 +142,16 @@ pub fn addOverflow(comptime T: type, a: T, b: T) -> %T {
}

fn getNumberWithDefault(s: []u8) -> u32 {
parseUnsigned(u32, s, 10) %% 42
parseUnsigned(u32, s, 10) catch 42
}

fn getNumberOrCrash(s: []u8) -> u32 {
%%parseUnsigned(u32, s, 10)
}

fn addTwoTogetherOrReturnErr(a_str: []u8, b_str: []u8) -> %u32 {
const a = parseUnsigned(u32, a_str, 10) %% |err| return err;
const b = parseUnsigned(u32, b_str, 10) %% |err| return err;
const a = parseUnsigned(u32, a_str, 10) catch |err| return err;
const b = parseUnsigned(u32, b_str, 10) catch |err| return err;
return a + b;
}</code></pre>
<h3 id="hashmap">HashMap with Custom Allocator</h3>
Expand Down Expand Up @@ -424,7 +424,7 @@ pub fn main() -&gt; %void {
} else if (arg[0] == '-') {
return usage(exe);
} else {
var is = io.InStream.open(arg, null) %% |err| {
var is = io.InStream.open(arg, null) catch |err| {
%%io.stderr.printf("Unable to open file: {}\n", @errorName(err));
return err;
};
Expand All @@ -449,7 +449,7 @@ fn cat_stream(is: &amp;io.InStream) -&gt; %void {
var buf: [1024 * 4]u8 = undefined;

while (true) {
const bytes_read = is.read(buf[0..]) %% |err| {
const bytes_read = is.read(buf[0..]) catch |err| {
%%io.stderr.printf("Unable to read from stream: {}\n", @errorName(err));
return err;
};
Expand All @@ -458,7 +458,7 @@ fn cat_stream(is: &amp;io.InStream) -&gt; %void {
break;
}

io.stdout.write(buf[0..bytes_read]) %% |err| {
io.stdout.write(buf[0..bytes_read]) catch |err| {
%%io.stderr.printf("Unable to write to stdout: {}\n", @errorName(err));
return err;
};
Expand Down
30 changes: 15 additions & 15 deletions doc/langref.html.in
Expand Up @@ -1211,8 +1211,8 @@ unwrapped == 1234</code></pre>
</td>
</tr>
<tr>
<td><pre><code class="zig">a %% b
a %% |err| b</code></pre></td>
<td><pre><code class="zig">a catch b
a catch |err| b</code></pre></td>
<td>
<ul>
<li><a href="#errors">Error Unions</a></li>
Expand All @@ -1226,7 +1226,7 @@ a %% |err| b</code></pre></td>
</td>
<td>
<pre><code class="zig">const value: %u32 = null;
const unwrapped = value %% 1234;
const unwrapped = value catch 1234;
unwrapped == 1234</code></pre>
</td>
</tr>
Expand All @@ -1238,7 +1238,7 @@ unwrapped == 1234</code></pre>
</ul>
</td>
<td>Equivalent to:
<pre><code class="zig">a %% unreachable</code></pre>
<pre><code class="zig">a catch unreachable</code></pre>
</td>
<td>
<pre><code class="zig">const value: %u32 = 5678;
Expand Down Expand Up @@ -1482,7 +1482,7 @@ x{}
== != &lt; &gt; &lt;= &gt;=
and
or
?? %%
?? catch
= *= /= %= += -= &lt;&lt;= &gt;&gt;= &amp;= ^= |=</code></pre>
<h2 id="arrays">Arrays</h2>
<pre><code class="zig">const assert = @import("std").debug.assert;
Expand Down Expand Up @@ -1829,7 +1829,7 @@ Test 1/1 pointer alignment safety...incorrect alignment
return root.main();
^
/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:47:13: 0x0000000000216050 in ??? (test)
callMain(argc, argv, envp) %% std.os.posix.exit(1);
callMain(argc, argv, envp) catch std.os.posix.exit(1);
^
/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:34:25: 0x0000000000215fa0 in ??? (test)
posixCallMainAndExit()
Expand Down Expand Up @@ -1885,7 +1885,7 @@ lib/zig/std/special/bootstrap.zig:60:21: 0x00000000002149e7 in ??? (test)
return root.main();
^
lib/zig/std/special/bootstrap.zig:47:13: 0x00000000002148a0 in ??? (test)
callMain(argc, argv, envp) %% std.os.posix.exit(1);
callMain(argc, argv, envp) catch std.os.posix.exit(1);
^
lib/zig/std/special/bootstrap.zig:34:25: 0x00000000002147f0 in ??? (test)
posixCallMainAndExit()
Expand Down Expand Up @@ -2965,7 +2965,7 @@ lib/zig/std/special/bootstrap.zig:60:21: 0x0000000000214947 in ??? (test)
return root.main();
^
lib/zig/std/special/bootstrap.zig:47:13: 0x0000000000214800 in ??? (test)
callMain(argc, argv, envp) %% std.os.posix.exit(1);
callMain(argc, argv, envp) catch std.os.posix.exit(1);
^
lib/zig/std/special/bootstrap.zig:34:25: 0x0000000000214750 in ??? (test)
posixCallMainAndExit()
Expand Down Expand Up @@ -3019,7 +3019,7 @@ extern fn bar(value: u32);</code></pre>
<pre><code class="zig">pub extern "kernel32" stdcallcc fn ExitProcess(exit_code: c_uint) -&gt; noreturn;

fn foo() {
const value = bar() %% ExitProcess(1);
const value = bar() catch ExitProcess(1);
assert(value == 1234);
}

Expand Down Expand Up @@ -3209,7 +3209,7 @@ pub fn parseU64(buf: []const u8, radix: u8) -&gt; %u64 {
</ul>
<p>If you want to provide a default value, you can use the <code>%%</code> binary operator:</p>
<pre><code class="zig">fn doAThing(str: []u8) {
const number = parseU64(str, 10) %% 13;
const number = parseU64(str, 10) catch 13;
// ...
}</code></pre>
<p>
Expand All @@ -3220,7 +3220,7 @@ pub fn parseU64(buf: []const u8, radix: u8) -&gt; %u64 {
<p>Let's say you wanted to return the error if you got one, otherwise continue with the
function logic:</p>
<pre><code class="zig">fn doAThing(str: []u8) -&gt; %void {
const number = parseU64(str, 10) %% |err| return err;
const number = parseU64(str, 10) catch |err| return err;
// ...
}</code></pre>
<p>
Expand All @@ -3239,7 +3239,7 @@ pub fn parseU64(buf: []const u8, radix: u8) -&gt; %u64 {
Maybe you know with complete certainty that an expression will never be an error.
In this case you can do this:
</p>
<pre><code class="zig">const number = parseU64("1234", 10) %% unreachable;</code></pre>
<pre><code class="zig">const number = parseU64("1234", 10) catch unreachable;</code></pre>
<p>
Here we know for sure that "1234" will parse successfully. So we put the
<code>unreachable</code> value on the right hand side. <code>unreachable</code> generates
Expand All @@ -3250,7 +3250,7 @@ pub fn parseU64(buf: []const u8, radix: u8) -&gt; %u64 {
<p>Again there is a syntactic shortcut for this:</p>
<pre><code class="zig">const number = %%parseU64("1234", 10);</code></pre>
<p>
The <code>%%</code> <em>prefix</em> operator is equivalent to <code class="zig">expression %% unreachable</code>. It unwraps an error union type,
The <code>%%</code> <em>prefix</em> operator is equivalent to <code class="zig">expression catch unreachable</code>. It unwraps an error union type,
and panics in debug mode if the value was an error.
</p>
<p>
Expand Down Expand Up @@ -4984,7 +4984,7 @@ Test 1/1 safety check...reached unreachable code
return root.main();
^
/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:37:13: 0x00000000002148d0 in ??? (test)
callMain(argc, argv, envp) %% exit(1);
callMain(argc, argv, envp) catch exit(1);
^
/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:30:20: 0x0000000000214820 in ??? (test)
callMainAndExit()
Expand Down Expand Up @@ -5909,7 +5909,7 @@ UnwrapExpression = BoolOrExpression (UnwrapNullable | UnwrapError) | BoolOrExpre

UnwrapNullable = "??" Expression

UnwrapError = "%%" option("|" Symbol "|") Expression
UnwrapError = "catch" option("|" Symbol "|") Expression

AssignmentExpression = UnwrapExpression AssignmentOperator UnwrapExpression | UnwrapExpression

Expand Down
8 changes: 4 additions & 4 deletions example/cat/main.zig
Expand Up @@ -20,7 +20,7 @@ pub fn main() -> %void {
} else if (arg[0] == '-') {
return usage(exe);
} else {
var file = io.File.openRead(arg, null) %% |err| {
var file = io.File.openRead(arg, null) catch |err| {
warn("Unable to open file: {}\n", @errorName(err));
return err;
};
Expand All @@ -45,7 +45,7 @@ fn cat_file(stdout: &io.File, file: &io.File) -> %void {
var buf: [1024 * 4]u8 = undefined;

while (true) {
const bytes_read = file.read(buf[0..]) %% |err| {
const bytes_read = file.read(buf[0..]) catch |err| {
warn("Unable to read from stream: {}\n", @errorName(err));
return err;
};
Expand All @@ -54,15 +54,15 @@ fn cat_file(stdout: &io.File, file: &io.File) -> %void {
break;
}

stdout.write(buf[0..bytes_read]) %% |err| {
stdout.write(buf[0..bytes_read]) catch |err| {
warn("Unable to write to stdout: {}\n", @errorName(err));
return err;
};
}
}

fn unwrapArg(arg: %[]u8) -> %[]u8 {
return arg %% |err| {
return arg catch |err| {
warn("Unable to parse command line: {}\n", err);
return err;
};
Expand Down
4 changes: 2 additions & 2 deletions example/guess_number/main.zig
Expand Up @@ -25,12 +25,12 @@ pub fn main() -> %void {
try stdout.print("\nGuess a number between 1 and 100: ");
var line_buf : [20]u8 = undefined;

const line_len = stdin_file.read(line_buf[0..]) %% |err| {
const line_len = stdin_file.read(line_buf[0..]) catch |err| {
try stdout.print("Unable to read from stdin: {}\n", @errorName(err));
return err;
};

const guess = fmt.parseUnsigned(u8, line_buf[0..line_len - 1], 10) %% {
const guess = fmt.parseUnsigned(u8, line_buf[0..line_len - 1], 10) catch {
try stdout.print("Invalid number.\n");
continue;
};
Expand Down
8 changes: 4 additions & 4 deletions src-self-hosted/main.zig
Expand Up @@ -21,7 +21,7 @@ error ZigInstallationNotFound;
const default_zig_cache_name = "zig-cache";

pub fn main() -> %void {
main2() %% |err| {
main2() catch |err| {
if (err != error.InvalidCommandLineArguments) {
warn("{}\n", @errorName(err));
}
Expand Down Expand Up @@ -571,12 +571,12 @@ fn printZen() -> %void {
/// Caller must free result
fn resolveZigLibDir(allocator: &mem.Allocator, zig_install_prefix_arg: ?[]const u8) -> %[]u8 {
if (zig_install_prefix_arg) |zig_install_prefix| {
return testZigInstallPrefix(allocator, zig_install_prefix) %% |err| {
return testZigInstallPrefix(allocator, zig_install_prefix) catch |err| {
warn("No Zig installation found at prefix {}: {}\n", zig_install_prefix_arg, @errorName(err));
return error.ZigInstallationNotFound;
};
} else {
return findZigLibDir(allocator) %% |err| {
return findZigLibDir(allocator) catch |err| {
warn("Unable to find zig lib directory: {}.\nReinstall Zig or use --zig-install-prefix.\n",
@errorName(err));
return error.ZigLibDirNotFound;
Expand Down Expand Up @@ -611,7 +611,7 @@ fn findZigLibDir(allocator: &mem.Allocator) -> %[]u8 {
break;
}

return testZigInstallPrefix(allocator, test_dir) %% |err| {
return testZigInstallPrefix(allocator, test_dir) catch |err| {
cur_path = test_dir;
continue;
};
Expand Down
4 changes: 2 additions & 2 deletions src-self-hosted/module.zig
Expand Up @@ -207,13 +207,13 @@ pub const Module = struct {
}

const root_src_path = self.root_src_path ?? @panic("TODO handle null root src path");
const root_src_real_path = os.path.real(self.allocator, root_src_path) %% |err| {
const root_src_real_path = os.path.real(self.allocator, root_src_path) catch |err| {
try printError("unable to get real path '{}': {}", root_src_path, err);
return err;
};
%defer self.allocator.free(root_src_real_path);

const source_code = io.readFileAllocExtra(root_src_real_path, self.allocator, 3) %% |err| {
const source_code = io.readFileAllocExtra(root_src_real_path, self.allocator, 3) catch |err| {
try printError("unable to open '{}': {}", root_src_real_path, err);
return err;
};
Expand Down

0 comments on commit 632d143

Please sign in to comment.