Add ex 35,36 enums; updated README
I'm changing the order of some more topics. Trying to explain the value of pointers when we're mostly dealing with stack-sized values like integers feels convoluted. So I'm starting with enums (which also has a nice segue from an earlier "switch" exercise). Then structs. Then unions (just in keeping with the order of these items on ziglearn.org) and THEN pointers and multi-pointers and slices.pull/2/head
parent
738a9f6cda
commit
2cded107cd
@ -0,0 +1,49 @@
|
|||||||
|
//
|
||||||
|
// Remember that little mathematical virtual machine we made using the
|
||||||
|
// "unreachable" statement? Well, there were two problems with the
|
||||||
|
// way we were using op codes:
|
||||||
|
//
|
||||||
|
// 1. Having to remember op codes by number is no good.
|
||||||
|
// 2. We had to use "unreachable" because Zig had no way of knowing
|
||||||
|
// how many valid op codes there were.
|
||||||
|
//
|
||||||
|
// An "enum" is a Zig construct that lets you give names to numeric
|
||||||
|
// values and store them in a set. They look a lot like error sets:
|
||||||
|
//
|
||||||
|
// const Fruit = enum{ apple, pear, orange };
|
||||||
|
//
|
||||||
|
// const my_fruit = Fruit.apple;
|
||||||
|
//
|
||||||
|
// Let's use an enum in place of the numbers we were using in the
|
||||||
|
// previous version!
|
||||||
|
//
|
||||||
|
const std = @import("std");
|
||||||
|
|
||||||
|
// Please complete the enum!
|
||||||
|
const Ops = enum{ ??? };
|
||||||
|
|
||||||
|
pub fn main() void {
|
||||||
|
const operations = [_]Ops{
|
||||||
|
Ops.inc,
|
||||||
|
Ops.inc,
|
||||||
|
Ops.inc,
|
||||||
|
Ops.pow,
|
||||||
|
Ops.dec,
|
||||||
|
Ops.dec
|
||||||
|
};
|
||||||
|
|
||||||
|
var current_value: u32 = 0;
|
||||||
|
|
||||||
|
for (operations) |op| {
|
||||||
|
switch (op) {
|
||||||
|
Ops.inc => { current_value += 1; },
|
||||||
|
Ops.dec => { current_value -= 1; },
|
||||||
|
Ops.pow => { current_value *= current_value; },
|
||||||
|
// No "else" needed! Why is that?
|
||||||
|
}
|
||||||
|
|
||||||
|
std.debug.print("{} ", .{current_value});
|
||||||
|
}
|
||||||
|
|
||||||
|
std.debug.print("\n", .{});
|
||||||
|
}
|
@ -0,0 +1,61 @@
|
|||||||
|
//
|
||||||
|
// Enums are really just a set of numbers. You can leave the
|
||||||
|
// numbering up to the compiler, or you can assign them
|
||||||
|
// explicitly. You can even specify the numeric type used.
|
||||||
|
//
|
||||||
|
// const Stuff = enum(u8){ foo = 16 };
|
||||||
|
//
|
||||||
|
// You can get the integer out with a built-in function:
|
||||||
|
//
|
||||||
|
// var my_stuff: u8 = @enumToInt(Stuff.foo);
|
||||||
|
//
|
||||||
|
// Note how that built-in function starts with "@" just like the
|
||||||
|
// @import() function we've been using.
|
||||||
|
//
|
||||||
|
const std = @import("std");
|
||||||
|
|
||||||
|
// Zig lets us write integers in hexadecimal format:
|
||||||
|
//
|
||||||
|
// 0xf (is the value 15 in hex)
|
||||||
|
//
|
||||||
|
// Web browsers let us specify colors using a hexadecimal
|
||||||
|
// number where each byte represents the brightness of the
|
||||||
|
// Red, Green, or Blue component (RGB) where two hex digits
|
||||||
|
// are one byte with a value range of 0-255:
|
||||||
|
//
|
||||||
|
// #RRGGBB
|
||||||
|
//
|
||||||
|
// Please define and use a pure blue value Color:
|
||||||
|
const Color = enum(u32){
|
||||||
|
red = 0xff0000,
|
||||||
|
green = 0x00ff00,
|
||||||
|
blue = ???,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn main() void {
|
||||||
|
// Remeber Zig's multi-line strings? Here they are again.
|
||||||
|
// Also, check out this cool format string:
|
||||||
|
//
|
||||||
|
// {x:0>6}
|
||||||
|
// ^
|
||||||
|
// x type ('x' is lower-case hexadecimal)
|
||||||
|
// : separator (needed for format syntax)
|
||||||
|
// 0 padding character (default is ' ')
|
||||||
|
// > alignment ('>' aligns right)
|
||||||
|
// 6 width (use padding to force width)
|
||||||
|
//
|
||||||
|
// Please add this formatting to the blue value.
|
||||||
|
// (Even better, experiment without it, or try parts of it
|
||||||
|
// to see what prints!)
|
||||||
|
std.debug.print(
|
||||||
|
\\<p>
|
||||||
|
\\ <span style="color: #{x:0>6}">Red</span>
|
||||||
|
\\ <span style="color: #{x:0>6}">Green</span>
|
||||||
|
\\ <span style="color: #{}">Blue</span>
|
||||||
|
\\</p>
|
||||||
|
, .{
|
||||||
|
@enumToInt(Color.red),
|
||||||
|
@enumToInt(Color.green),
|
||||||
|
@enumToInt(???), // Oops! We're missing something!
|
||||||
|
});
|
||||||
|
}
|
Loading…
Reference in New Issue