diff options
Diffstat (limited to 'exercises')
| -rw-r--r-- | exercises/03_assignment.zig | 2 | ||||
| -rw-r--r-- | exercises/19_functions2.zig | 2 | ||||
| -rw-r--r-- | exercises/22_errors2.zig | 2 | ||||
| -rw-r--r-- | exercises/45_optionals.zig | 52 |
4 files changed, 55 insertions, 3 deletions
diff --git a/exercises/03_assignment.zig b/exercises/03_assignment.zig index 662fd18..3775afc 100644 --- a/exercises/03_assignment.zig +++ b/exercises/03_assignment.zig @@ -15,7 +15,7 @@ // bar CAN be negative and can hold −128 to 127 // // const foo: u8 = 20; -// var bar: i8 = -20; +// const bar: i8 = -20; // // Example: foo can hold 8 bits (0 to 255) // bar can hold 16 bits (0 to 65,535) diff --git a/exercises/19_functions2.zig b/exercises/19_functions2.zig index 4d195a7..99319c3 100644 --- a/exercises/19_functions2.zig +++ b/exercises/19_functions2.zig @@ -1,7 +1,7 @@ // // Now let's create a function that takes a parameter. Here's an // example that takes two parameters. As you can see, parameters -// are declared just like an other types ("name": "type"): +// are declared just like any other types ("name": "type"): // // fn myFunction( number: u8, is_lucky: bool ) { // ... diff --git a/exercises/22_errors2.zig b/exercises/22_errors2.zig index 0f8571f..7675d2d 100644 --- a/exercises/22_errors2.zig +++ b/exercises/22_errors2.zig @@ -10,7 +10,7 @@ // Zig lets us make what's called an "error union" which is a value // which could either be a regular value OR an error from a set: // -// var text: MyErrorSet!Text = getText('foo.txt'); +// var text: MyErrorSet!Text = getText('foo.txt'); // // For now, let's just see if we can try making an error union! // diff --git a/exercises/45_optionals.zig b/exercises/45_optionals.zig new file mode 100644 index 0000000..815ba75 --- /dev/null +++ b/exercises/45_optionals.zig @@ -0,0 +1,52 @@ +// +// Sometimes you know that a variable might hold a value or +// it might not. Zig has a neat way of expressing this idea +// called Optionals. An optional type just has a '?' like this: +// +// var foo: ?u32 = 10; +// +// Now foo can store a u32 integer OR null (a value storing +// the cosmic horror of a value NOT EXISTING!) +// +// foo = null; +// +// if (foo == null) beginScreaming(); +// +// Before we can use the optional value as the non-null type +// (a u32 integer in this case), we need to guarantee that it +// isn't null. One way to do this is to THREATEN IT with the +// "orelse" statement. +// +// var bar = foo orelse 2; +// +// Here, bar will either equal the u32 integer value stored in +// foo, or it will equal 2 if foo was null. +// +const std = @import("std"); + +pub fn main() void { + const result = deepThought(); + + // Please threaten the result so that answer is either the + // integer value from deepThought() OR the number 42: + var answer: u8 = result; + + std.debug.print("The Ultimate Answer: {}.\n",.{answer}); +} + +fn deepThought() ?u8 { + // It seems Deep Thought's output has declined in quality. + // But we'll leave this as-is. Sorry Deep Thought. + return null; +} +// +// Blast from the past: +// +// Optionals are a lot like error union types which can either +// hold a value or an error. Likewise, the orelse statement is +// like the catch statement used to "unwrap" a value or supply +// a default value: +// +// var maybe_bad: Error!u32 = Error.Evil; +// var number: u32 = maybe_bad catch 0; +// |
