2017-01-05 00:57:48 -08:00
|
|
|
const assert = @import("std").debug.assert;
|
|
|
|
|
2017-05-23 18:38:31 -07:00
|
|
|
test "nullable type" {
|
2017-05-07 20:25:36 -07:00
|
|
|
const x : ?bool = true;
|
2016-12-21 22:20:08 -08:00
|
|
|
|
2017-05-03 14:23:11 -07:00
|
|
|
if (x) |y| {
|
2016-12-21 22:20:08 -08:00
|
|
|
if (y) {
|
|
|
|
// OK
|
|
|
|
} else {
|
2017-03-26 01:58:48 -07:00
|
|
|
unreachable;
|
2016-12-21 22:20:08 -08:00
|
|
|
}
|
|
|
|
} else {
|
2017-03-26 01:58:48 -07:00
|
|
|
unreachable;
|
2016-12-21 22:20:08 -08:00
|
|
|
}
|
|
|
|
|
2017-05-07 20:25:36 -07:00
|
|
|
const next_x : ?i32 = null;
|
2016-12-21 22:20:08 -08:00
|
|
|
|
|
|
|
const z = next_x ?? 1234;
|
|
|
|
|
|
|
|
assert(z == 1234);
|
|
|
|
|
2017-05-07 20:25:36 -07:00
|
|
|
const final_x : ?i32 = 13;
|
2016-12-21 22:20:08 -08:00
|
|
|
|
2017-03-26 01:58:48 -07:00
|
|
|
const num = final_x ?? unreachable;
|
2016-12-21 22:20:08 -08:00
|
|
|
|
|
|
|
assert(num == 13);
|
|
|
|
}
|
|
|
|
|
2017-04-21 13:46:33 -07:00
|
|
|
test "test maybe object and get a pointer to the inner value" {
|
2016-12-21 22:42:30 -08:00
|
|
|
var maybe_bool: ?bool = true;
|
|
|
|
|
2017-05-03 14:23:11 -07:00
|
|
|
if (maybe_bool) |*b| {
|
2016-12-21 22:42:30 -08:00
|
|
|
*b = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(??maybe_bool == false);
|
|
|
|
}
|
|
|
|
|
2017-04-21 13:46:33 -07:00
|
|
|
|
2017-05-23 18:38:31 -07:00
|
|
|
test "rhs maybe unwrap return" {
|
2017-05-07 20:25:36 -07:00
|
|
|
const x: ?bool = true;
|
2016-12-22 07:09:53 -08:00
|
|
|
const y = x ?? return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-21 12:08:03 -07:00
|
|
|
test "maybe return" {
|
2017-03-10 08:21:41 -08:00
|
|
|
maybeReturnImpl();
|
|
|
|
comptime maybeReturnImpl();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn maybeReturnImpl() {
|
2016-12-22 07:09:53 -08:00
|
|
|
assert(??foo(1235));
|
2017-05-03 14:23:11 -07:00
|
|
|
if (foo(null) != null)
|
2017-04-21 13:46:33 -07:00
|
|
|
unreachable;
|
2016-12-22 07:09:53 -08:00
|
|
|
assert(!??foo(1234));
|
|
|
|
}
|
|
|
|
|
2017-03-10 08:21:41 -08:00
|
|
|
fn foo(x: ?i32) -> ?bool {
|
2017-04-21 12:08:03 -07:00
|
|
|
const value = x ?? return null;
|
2017-03-10 08:21:41 -08:00
|
|
|
return value > 1234;
|
|
|
|
}
|
|
|
|
|
2016-12-26 00:44:59 -08:00
|
|
|
|
2017-05-23 18:38:31 -07:00
|
|
|
test "if var maybe pointer" {
|
2016-12-26 00:44:59 -08:00
|
|
|
assert(shouldBeAPlus1(Particle {.a = 14, .b = 1, .c = 1, .d = 1}) == 15);
|
|
|
|
}
|
2017-03-26 00:39:18 -07:00
|
|
|
fn shouldBeAPlus1(p: &const Particle) -> u64 {
|
|
|
|
var maybe_particle: ?Particle = *p;
|
2017-05-03 14:23:11 -07:00
|
|
|
if (maybe_particle) |*particle| {
|
2016-12-26 00:44:59 -08:00
|
|
|
particle.a += 1;
|
|
|
|
}
|
2017-05-03 14:23:11 -07:00
|
|
|
if (maybe_particle) |particle| {
|
2016-12-26 00:44:59 -08:00
|
|
|
return particle.a;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
const Particle = struct {
|
|
|
|
a: u64,
|
|
|
|
b: u64,
|
|
|
|
c: u64,
|
|
|
|
d: u64,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-05-23 18:38:31 -07:00
|
|
|
test "null literal outside function" {
|
2017-01-16 13:39:31 -08:00
|
|
|
const is_null = here_is_a_null_literal.context == null;
|
2016-12-26 00:44:59 -08:00
|
|
|
assert(is_null);
|
2017-01-16 13:39:31 -08:00
|
|
|
|
|
|
|
const is_non_null = here_is_a_null_literal.context != null;
|
|
|
|
assert(!is_non_null);
|
2016-12-26 00:44:59 -08:00
|
|
|
}
|
|
|
|
const SillyStruct = struct {
|
|
|
|
context: ?i32,
|
|
|
|
};
|
|
|
|
const here_is_a_null_literal = SillyStruct {
|
|
|
|
.context = null,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-05-23 18:38:31 -07:00
|
|
|
test "test null runtime" {
|
2017-01-16 13:39:31 -08:00
|
|
|
testTestNullRuntime(null);
|
|
|
|
}
|
|
|
|
fn testTestNullRuntime(x: ?i32) {
|
|
|
|
assert(x == null);
|
|
|
|
assert(!(x != null));
|
|
|
|
}
|
2017-03-10 08:21:41 -08:00
|
|
|
|
2017-05-23 18:38:31 -07:00
|
|
|
test "nullable void" {
|
2017-03-10 08:21:41 -08:00
|
|
|
nullableVoidImpl();
|
|
|
|
comptime nullableVoidImpl();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn nullableVoidImpl() {
|
|
|
|
assert(bar(null) == null);
|
|
|
|
assert(bar({}) != null);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bar(x: ?void) -> ?void {
|
2017-05-03 14:23:11 -07:00
|
|
|
if (x) |_| {
|
2017-03-10 08:21:41 -08:00
|
|
|
return {};
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2017-05-29 11:08:39 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const StructWithNullable = struct {
|
|
|
|
field: ?i32,
|
|
|
|
};
|
|
|
|
|
|
|
|
var struct_with_nullable: StructWithNullable = undefined;
|
|
|
|
|
|
|
|
test "unwrap nullable which is field of global var" {
|
|
|
|
struct_with_nullable.field = null;
|
|
|
|
if (struct_with_nullable.field) |payload| {
|
|
|
|
unreachable;
|
|
|
|
}
|
|
|
|
struct_with_nullable.field = 1234;
|
|
|
|
if (struct_with_nullable.field) |payload| {
|
|
|
|
assert(payload == 1234);
|
|
|
|
} else {
|
|
|
|
unreachable;
|
|
|
|
}
|
|
|
|
}
|
2017-06-14 16:32:52 -07:00
|
|
|
|
|
|
|
test "null with default unwrap" {
|
|
|
|
const x: i32 = null ?? 1;
|
|
|
|
assert(x == 1);
|
|
|
|
}
|