Learning Zig from the official language reference.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
zig-langref/optionals.zig

20 lines
497 B

const std = @import("std");
const debug = std.debug;
const testing = std.testing;
test "optional type reflection" {
var foo: ?i32 = null;
testing.expect(foo == null);
foo = 12345;
testing.expectEqual(foo.?, 12345);
testing.expectEqual(@typeInfo(@TypeOf(foo)).Optional.child, i32);
}
test "optional pointers" {
var ptr: ?*i32 = null;
var x: i32 = 42;
ptr = &x;
testing.expectEqual(ptr.?.*, x);
testing.expectEqual(@sizeOf(@TypeOf(ptr)), @sizeOf(*i32));
}