#Zigling 46 (046_optionals2)

1 messages Β· Page 1 of 1 (latest)

hardy ivy
#

Hi πŸ‘‹πŸ» , I'm trying to complete Zigling 46. I've changed the type of 'tail' in the struct to be an optional pointer. My understanding is that for elephantC the tail will be null and by using e.tail.? it should jump into 'unreachable' and do nothing.

Could someone please point out where I'm going wrong?

**The output: **
Compiling: 046_optionals2.zig
Checking: 046_optionals2.zig
error: 046_optionals2.zig terminated unexpectedly

My Zig version:
zig version 0.13.0

The code
`const std = @import("std");

const Elephant = struct {
letter: u8,
tail: ?*Elephant = null, // Hmm... tail needs something...
visited: bool = false,
};

pub fn main() void {
var elephantA = Elephant{ .letter = 'A' };
var elephantB = Elephant{ .letter = 'B' };
var elephantC = Elephant{ .letter = 'C' };

// Link the elephants so that each tail "points" to the next.
elephantA.tail = &elephantB;
elephantB.tail = &elephantC;

visitElephants(&elephantA);

std.debug.print("\n", .{});

}

// This function visits all elephants once, starting with the
// first elephant and following the tails to the next elephant.
fn visitElephants(first_elephant: *Elephant) void {
var e = first_elephant;

while (!e.visited) {
    std.debug.print("Elephant {u}. ", .{e.letter});
    e.visited = true;

    // We should stop once we encounter a tail that
    // does NOT point to another element. What can
    // we put here to make that happen?
    e = e.tail.?;
}

}`

lapis atlas
#

what does the compile error say?

hardy ivy
#

There isn't one really, all i'm getting is

Compiling: 046_optionals2.zig
Checking: 046_optionals2.zig
error: 046_optionals2.zig terminated unexpectedly

Ziglings hint: Elephants again!
Edit exercises/046_optionals2.zig and run 'zig build' again.

lapis atlas
#

hmmm
tail: ?Elephant = null, // Hmm... tail needs something...
tail is null or Elephant
but here elephantA.tail = &elephantB; you are setting tail to the pointer of an Elephant
those are differnt types and should cause a compile error
?Elephant cant accept values of *Elephant

hardy ivy
#

Ah that's a good catch, but it seems like I made a copy/pasting error. In my editor the line looks like this:

tail: ?*Elephant = null, // Hmm... tail needs something...

lapis atlas
#

also e.tail.? it should jump into "unreachable"' unreacheble` will alwasy cause a panic and the program to crash

#

it does not cause a return or beark out of the loop

#

it is ment to be used in places that should not be possible to each so that the program crashes if something goes wrong

#

In Debug and ReleaseSafe mode unreachable emits a call to panic with the message reached unreachable code.

In ReleaseFast and ReleaseSmall mode, the optimizer uses the assumption that unreachable code will never be hit to perform optimizations.
from the documentation

hardy ivy
#

Ah I see, that explains the output I'm getting as well. It Seems like I misunderstood unreachable and have to think a little harder on how to solve this Zigling. Thanks for pointing that out!