-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
fmt.formatType() doesn't know about (undefined) #8521
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I'm having a hard time coming up with a use case for why someone would want to print |
It looks like you can't even pass |
@mlawren:
// case 1:
const Point = struct {
x: u32,
y: u32,
};
var pt1 = Point { .x = 10, .y = undefined };
var pt2 = Point { .x = 20, .y = undefined };
return pt1.x == pt2.x;
// we never touch .y, so we can leave it undefined // case 2:
const Ring = struct {
value: u32,
next: *Ring,
};
var r1 = Ring {
.value = 1,
.next = undefined, // we are not able to fill this yet
};
var r2 = Ring {
.value = 2,
.next = &r1,
};
r1.next = &r2; // now we can fill this "hole" in the data structure Also note that you are not allowed to compare to |
Thanks for the replies and the education - yes, I'm new to Zig. The use case here is simply debugging while I am learning, but I can also imagine wanting to check if a value as been assigned or not in more complicated code paths. |
If you want to be able to check, use optional types: https://ziglang.org/documentation/master/#Optionals |
I'm new to zig too, got this while learning(or attempting to learn by doing after watching this video https://www.youtube.com/watch?v=I2E-9jSsxcM ):
const print = std.debug.print;
const std = @import("std");
pub fn main() !void {
const undef = undefined;
print("Hi! {s}\n", .{undef});
}
I'm kinda surprised that this isn't gonna get fixed or something... EDIT: aww man, I thought zig was better: const print = std.debug.print;
const std = @import("std");
pub fn main() !void {
const u: u8 = undefined; //XXX: if this becomes 'var' then no error below:
var w: u8 = u + 1; // undef-tst.zig:7:17: error: use of undefined value here causes undefined behavior
print("Hi! {}\n", .{u});
print("Hi! {}\n", .{u == undefined}); // false
print("Hi! {}\n", .{undefined == u}); // true
// XXX: ^ wtf?
//print("Hi! {}\n", .{undefined == undefined}); // error: operator == not allowed for type '@TypeOf(undefined)' //This makes some sense.
u = u + 1;
print("Hi! {}\n", .{w});
}
"version": "0.10.0-dev.3857+10e11b60e" |
@correabuscar you cannot expect Zig to ever do anything meaningful with Please read my comment above. I assume you're coming from JavaScript world where
It will never change, because there's nothing to fix. Please read the language documentation to learn more about |
Thank you.
The fix I would've expected was a better compiler error message. Maybe something saying that using Heck, the compiler errors(verb) for simple things like unused variables, which is great in my opinion. But it somehow allows using I guess maybe it's not easy to restrict the use of
Though I do expect the compiler to prevent me from using it outside of just initialization.
Seems to have a "type" though: const r: @TypeOf(undefined) = undefined;
_ = r; (I don't really know Javascript though, but I've stumbled upon its But please, I don't want anything to change on my account. I'm merely expressing my, well, disappointment with however (wrongly) I'm understanding Zig at this point, and as I'm not a developer, you're not losing anything or anyone because of this. And really, keep up the great work, even though Zig isn't for me(apparently), it's for thousands of others and I wish all the best to them and to you all! Keep up the great work!
|
I would recommend reading #1947 to better understand how
That is a bug #10703 |
The text was updated successfully, but these errors were encountered: