mirror of
https://codeberg.org/andyscott/ziglings.git
synced 2024-12-22 14:03:10 -05:00
add ex073 comptime 8
This commit is contained in:
parent
2ecf31daa4
commit
db20e9541a
3 changed files with 77 additions and 0 deletions
|
@ -366,6 +366,10 @@ const exercises = [_]Exercise{
|
|||
.main_file = "072_comptime7.zig",
|
||||
.output = "26",
|
||||
},
|
||||
.{
|
||||
.main_file = "073_comptime8.zig",
|
||||
.output = "My llama value is 25.",
|
||||
},
|
||||
};
|
||||
|
||||
/// Check the zig version to make sure it can compile the examples properly.
|
||||
|
|
65
exercises/073_comptime8.zig
Normal file
65
exercises/073_comptime8.zig
Normal file
|
@ -0,0 +1,65 @@
|
|||
//
|
||||
// As a matter of fact, you can put 'comptime' in front of any
|
||||
// expression to force it to be run at compile time.
|
||||
//
|
||||
// Execute a function:
|
||||
//
|
||||
// comptime llama();
|
||||
//
|
||||
// Get a value:
|
||||
//
|
||||
// bar = comptime baz();
|
||||
//
|
||||
// Execute a whole block:
|
||||
//
|
||||
// comptime {
|
||||
// bar = baz + biff();
|
||||
// llama(bar);
|
||||
// }
|
||||
//
|
||||
// Get a value from a block:
|
||||
//
|
||||
// var llama = comptime bar: {
|
||||
// const baz = biff() + bonk();
|
||||
// break :bar baz;
|
||||
// }
|
||||
//
|
||||
const print = @import("std").debug.print;
|
||||
|
||||
const llama_count = 5;
|
||||
const llamas = [llama_count]u32{ 5, 10, 15, 20, 25 };
|
||||
|
||||
pub fn main() void {
|
||||
// We meant to fetch the last llama. Please fix this simple
|
||||
// mistake so the assertion no longer fails.
|
||||
const my_llama = getLlama(5);
|
||||
|
||||
print("My llama value is {}.\n", .{my_llama});
|
||||
}
|
||||
|
||||
fn getLlama(i: usize) u32 {
|
||||
// We've put a guard assert() at the top of this function to
|
||||
// prevent mistakes. The 'comptime' keyword here means that
|
||||
// the mistake will be caught when we compile!
|
||||
//
|
||||
// Without 'comptime', this would still work, but the
|
||||
// assertion would fail at runtime with a PANIC, and that's
|
||||
// not as nice.
|
||||
//
|
||||
// Unfortunately, we're going to get an error right now
|
||||
// because the 'i' parameter needs to be guaranteed to be
|
||||
// known at compile time. What can you do with the 'i'
|
||||
// parameter above to make this so?
|
||||
comptime assert(i < llama_count);
|
||||
|
||||
return llamas[i];
|
||||
}
|
||||
|
||||
// Fun fact: this assert() function is identical to
|
||||
// std.debug.assert() from the Zig Standard Library.
|
||||
fn assert(ok: bool) void {
|
||||
if (!ok) unreachable;
|
||||
}
|
||||
//
|
||||
// Bonus fun fact: I accidentally replaced all instances of 'foo'
|
||||
// with 'llama' in this exercise and I have no regrets!
|
8
patches/patches/073_comptime8.patch
Normal file
8
patches/patches/073_comptime8.patch
Normal file
|
@ -0,0 +1,8 @@
|
|||
35c35
|
||||
< const my_llama = getLlama(5);
|
||||
---
|
||||
> const my_llama = getLlama(4);
|
||||
40c40
|
||||
< fn getLlama(i: usize) u32 {
|
||||
---
|
||||
> fn getLlama(comptime i: usize) u32 {
|
Loading…
Reference in a new issue