Code Monkey home page Code Monkey logo

Comments (7)

ratfactor avatar ratfactor commented on August 15, 2024

@charlieman First off, I'm delighted that you've enjoyed Ziglings so far and I'm glad you figured out the real answer - making tail an optional value with ?.

This null vs undefined is pretty fascinating. I hadn't thought of it that way. You're absolutely right that setting the default value of the tail field to null rather than undefined works just fine. That part makes sense.

But what really surprised me was that I could put undefined in this condition as well:

if (e.tail == undefined) break;

My understanding is that null is a value meaning "no value" where undefined is a placeholder telling the compiler (in a contractual sense) that this value doesn't exist and must never be used.

At the very least, I might need to add an additional comment on this exercise. Do you think adding a hint about using the ?<type> syntax we learned in the previous exercise would do the trick?

In the meantime, I'm going to see if my understanding of the undefined keyword needs some refinement. 😄

Thanks!

from ziglings.

ratfactor avatar ratfactor commented on August 15, 2024

Ha ha. Just our luck, I think undefined being allowed in the condition in this one case is a Zig bug. I've filed an issue: ziglang/zig#8056

In the meantime, I think my understanding wasn't entirely wrong, so let me know what you think about adding the hint.

from ziglings.

charlieman avatar charlieman commented on August 15, 2024

Neat, I'm thrilled my question led to a bug in the compiler itself.

I don't think the extra hint is necessary, but maybe the exercise could use tail: *Elephant = null to avoid the confusion?

If I try to print elephantA.tail to the console right after creating it:

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

It prints null whether the default is null or undefined, so it seems both use null internally?

from ziglings.

charlieman avatar charlieman commented on August 15, 2024

Wait, I take that last part back. With this little example it prints u8@aaaaaaaaaaaaaaaa:

const std = @import("std");
const Foo = struct {
    a: ?*u8 = undefined,
    //b: ?*u8 = null,
};
pub fn main() void {
    var foo = Foo{};
    std.debug.print("{}", .{foo.a});
}

However if I uncomment b it prints null. Maybe I shouldn't trust the print output that much. 😕

from ziglings.

ifreund avatar ifreund commented on August 15, 2024

It prints null whether the default is null or undefined, so it seems both use null internally?

as you seem to have discovered judging by your next comment, the value of undefined is, as one might expect from the keyword, undefined. That is to say that the value could be literally anything and that branching on this value is a bug in all cases.

In safe build modes (debug and release safe) zig uses 0xaa as the value for undefined bytes to make spotting undefined values when using a debugger easier. This is merely an implementation detail however and should never be relied upon in correct code.

from ziglings.

ratfactor avatar ratfactor commented on August 15, 2024

After hemming and hawing over this for a while, I agree that changing the default value in the definition to null will help a lot with making this exercise less confusing! Thanks again @charlieman for this observation.

from ziglings.

ratfactor avatar ratfactor commented on August 15, 2024

Thanks @ifreund ! This exercise and some additional semantic issues that have arisen on my Zig journey recently have made it clear to me that after these struct/method exercises, we need a couple more that focus on the fundamentals of assigning and passing values (and null vs undefined).

from ziglings.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.