Skip to content

Commit 95bdff5

Browse files
committed
Add ex 50 'no values' (help further address #25)
1 parent 2d205d9 commit 95bdff5

File tree

3 files changed

+102
-1
lines changed

3 files changed

+102
-1
lines changed

build.zig

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,10 @@ const exercises = [_]Exercise{
260260
.output = "A B C Cv Bv Av",
261261
.hint = "Now you're writting Zig!",
262262
},
263-
// 50 null vs undefined
263+
.{
264+
.main_file = "50_no_value.zig",
265+
.output = "That is not dead which can eternal lie / And with strange aeons even death may die.",
266+
},
264267
// 51 pass-by-value and const fn params
265268
// 52 slices!
266269
};

exercises/50_no_value.zig

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
//
2+
// "We live on a placid island of ignorance in the midst
3+
// of black seas of infinity, and it was not meant that
4+
// we should voyage far."
5+
//
6+
// from The Call of Cthulhu
7+
// by H. P. Lovecraft
8+
//
9+
// Zig has at least four ways of expressing "no value":
10+
//
11+
// * undefined
12+
//
13+
// var foo: u8 = undefined;
14+
//
15+
// "undefined" should not be thought of as a value, but as a way
16+
// of telling the compiler that you are not assigning a value
17+
// _yet_. Any type may be set to undefined, but attempting
18+
// to read or use that value is _always_ a mistake.
19+
//
20+
// * null
21+
//
22+
// var foo: ?u8 = null;
23+
//
24+
// The "null" primitive value _is_ a value that means "no value".
25+
// This is typically used with optional types as with the ?u8
26+
// shown above. When foo equals null, that's not a value of type
27+
// u8. It means there is _no value_ of type u8 in foo at all!
28+
//
29+
// * error
30+
//
31+
// var foo: MyError!u8 = BadError;
32+
//
33+
// Errors are _very_ similar to nulls. They _are_ a value, but
34+
// they usually indicate that the "real value" you were looking
35+
// for does not exist. Instead, you have an error. The example
36+
// error union type of MyError!u8 means that foo either holds
37+
// a u8 value OR an error. There is _no value_ of type u8 in foo
38+
// when it's set to an error!
39+
//
40+
// * void
41+
//
42+
// var foo: void = {};
43+
//
44+
// "void" is a _type_, not a value. It is the most popular of the
45+
// Zero Bit Types (those types which take up absolutely no space
46+
// and have only a semantic value. When compiled to executable
47+
// code, zero bit types generate no code at all. The above example
48+
// shows a variable foo of type void which is assigned the value
49+
// of an empty expression. It's much more common to see void as
50+
// the return type of a function that returns nothing.
51+
//
52+
// Zig has all of these ways of expressing different types of "no value"
53+
// because they each serve a purpose. Briefly:
54+
//
55+
// * undefined - there is no value YET, this cannot be read YET
56+
// * null - there is an explicit value of "no value"
57+
// * errors - there is no value because something went wrong
58+
// * void - there will NEVER be a value stored here
59+
//
60+
// Please use the correct "no value" for each ??? to make this program
61+
// print out a cursed quote from the Necronomicon. ...If you dare.
62+
//
63+
const std = @import("std");
64+
65+
const Err = error{Cthulhu};
66+
67+
pub fn main() void {
68+
var first_line1: *const [16]u8 = ???;
69+
first_line1 = "That is not dead";
70+
71+
var first_line2: Err!*const [21]u8 = ???;
72+
first_line2 = "which can eternal lie";
73+
74+
std.debug.print("{s} {s} / ", .{ first_line1, first_line2 });
75+
76+
printSecondLine();
77+
}
78+
79+
fn printSecondLine() ??? {
80+
var second_line2: ?*const [18]u8 = ???;
81+
second_line2 = "even death may die";
82+
83+
std.debug.print("And with strange aeons {s}.\n", .{second_line2.?});
84+
}

patches/patches/50_no_value.patch

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
68c68
2+
< var first_line1: *const [16]u8 = ???;
3+
---
4+
> var first_line1: *const [16]u8 = undefined;
5+
71c71
6+
< var first_line2: Err!*const [21]u8 = ???;
7+
---
8+
> var first_line2: Err!*const [21]u8 = Err.Cthulhu;
9+
79,80c79,80
10+
< fn printSecondLine() ??? {
11+
< var second_line2: ?*const [18]u8 = ???;
12+
---
13+
> fn printSecondLine() void {
14+
> var second_line2: ?*const [18]u8 = null;

0 commit comments

Comments
 (0)