Comments (7)
@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.
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.
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.
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.
It prints
null
whether the default isnull
orundefined
, 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.
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.
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)
- Use `role` instead of `class` for struct related exercises HOT 1
- error on build with latest zig 0.11.0-dev.4229 HOT 1
- 062_loop_expressions comptime-only type error HOT 1
- expected 2 arguments and 1 found HOT 6
- Explain abbreviation `lhs`
- 101: More details for SoA vs AoS HOT 4
- Exercise 18 fails in new version (0.12.0) HOT 2
- How do you prevent ziglings from running previously solved exercises? HOT 4
- Zigling 045 HOT 1
- [Exercise 48] Can anyone explain what is happening here? HOT 1
- Announcement! We've moved to Codeberg HOT 16
- Excercise to perform input via console HOT 1
- Ziglings broken by latest Master HOT 2
- Understanding Exercise 40 note
- Failing compilation for 0.12.0-dev.1632+acf9de376 HOT 2
- Set up GitHub mirror HOT 6
- Cannot build with "zig build" HOT 4
- Mistake in 094? HOT 2
- exercise/065_builtins2.zig not compiling HOT 2
- Adding JSON & Hashmap Ziglings HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ziglings.