2017-01-05 00:57:48 -08:00
|
|
|
const assert = @import("std").debug.assert;
|
|
|
|
|
2017-03-16 13:02:35 -07:00
|
|
|
test "simpleGenericFn" {
|
2016-12-18 21:41:37 -08:00
|
|
|
assert(max(i32, 3, -1) == 3);
|
|
|
|
assert(max(f32, 0.123, 0.456) == 0.456);
|
|
|
|
assert(add(2, 3) == 5);
|
|
|
|
}
|
|
|
|
|
2017-01-22 16:51:37 -08:00
|
|
|
fn max(comptime T: type, a: T, b: T) -> T {
|
2016-12-18 21:41:37 -08:00
|
|
|
return if (a > b) a else b;
|
|
|
|
}
|
|
|
|
|
2017-01-22 16:51:37 -08:00
|
|
|
fn add(comptime a: i32, b: i32) -> i32 {
|
2017-01-23 20:30:20 -08:00
|
|
|
return (comptime {a}) + b;
|
2016-12-18 21:41:37 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
const the_max = max(u32, 1234, 5678);
|
2017-03-16 13:02:35 -07:00
|
|
|
test "compileTimeGenericEval" {
|
2016-12-18 21:41:37 -08:00
|
|
|
assert(the_max == 5678);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn gimmeTheBigOne(a: u32, b: u32) -> u32 {
|
|
|
|
max(u32, a, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn shouldCallSameInstance(a: u32, b: u32) -> u32 {
|
|
|
|
max(u32, a, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn sameButWithFloats(a: f64, b: f64) -> f64 {
|
|
|
|
max(f64, a, b)
|
|
|
|
}
|
|
|
|
|
2017-03-16 13:02:35 -07:00
|
|
|
test "fnWithInlineArgs" {
|
2016-12-18 21:41:37 -08:00
|
|
|
assert(gimmeTheBigOne(1234, 5678) == 5678);
|
|
|
|
assert(shouldCallSameInstance(34, 12) == 34);
|
|
|
|
assert(sameButWithFloats(0.43, 0.49) == 0.49);
|
|
|
|
}
|
|
|
|
|
2016-12-21 22:20:08 -08:00
|
|
|
|
2017-03-16 13:02:35 -07:00
|
|
|
test "varParams" {
|
2016-12-21 22:20:08 -08:00
|
|
|
assert(max_i32(12, 34) == 34);
|
|
|
|
assert(max_f64(1.2, 3.4) == 3.4);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO `_`
|
|
|
|
const _1 = assert(max_i32(12, 34) == 34);
|
|
|
|
const _2 = assert(max_f64(1.2, 3.4) == 3.4);
|
|
|
|
|
|
|
|
fn max_var(a: var, b: var) -> @typeOf(a + b) {
|
|
|
|
if (a > b) a else b
|
|
|
|
}
|
|
|
|
|
|
|
|
fn max_i32(a: i32, b: i32) -> i32 {
|
|
|
|
max_var(a, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn max_f64(a: f64, b: f64) -> f64 {
|
|
|
|
max_var(a, b)
|
|
|
|
}
|
|
|
|
|
2016-12-22 07:09:53 -08:00
|
|
|
|
2017-01-22 16:51:37 -08:00
|
|
|
pub fn List(comptime T: type) -> type {
|
2016-12-22 07:09:53 -08:00
|
|
|
SmallList(T, 8)
|
|
|
|
}
|
|
|
|
|
2017-01-22 16:51:37 -08:00
|
|
|
pub fn SmallList(comptime T: type, comptime STATIC_SIZE: usize) -> type {
|
2016-12-22 07:09:53 -08:00
|
|
|
struct {
|
|
|
|
items: []T,
|
|
|
|
length: usize,
|
|
|
|
prealloc_items: [STATIC_SIZE]T,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-16 13:02:35 -07:00
|
|
|
test "functionWithReturnTypeType" {
|
2016-12-22 07:09:53 -08:00
|
|
|
var list: List(i32) = undefined;
|
|
|
|
var list2: List(i32) = undefined;
|
|
|
|
list.length = 10;
|
|
|
|
list2.length = 10;
|
|
|
|
assert(list.prealloc_items.len == 8);
|
|
|
|
assert(list2.prealloc_items.len == 8);
|
|
|
|
}
|
|
|
|
|
2016-12-26 00:44:59 -08:00
|
|
|
|
2017-03-16 13:02:35 -07:00
|
|
|
test "genericStruct" {
|
2016-12-26 00:44:59 -08:00
|
|
|
var a1 = GenNode(i32) {.value = 13, .next = null,};
|
|
|
|
var b1 = GenNode(bool) {.value = true, .next = null,};
|
|
|
|
assert(a1.value == 13);
|
|
|
|
assert(a1.value == a1.getVal());
|
|
|
|
assert(b1.getVal());
|
|
|
|
}
|
2017-01-22 16:51:37 -08:00
|
|
|
fn GenNode(comptime T: type) -> type {
|
2016-12-26 00:44:59 -08:00
|
|
|
struct {
|
|
|
|
value: T,
|
|
|
|
next: ?&GenNode(T),
|
|
|
|
fn getVal(n: &const GenNode(T)) -> T { n.value }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-16 13:02:35 -07:00
|
|
|
test "constDeclsInStruct" {
|
2016-12-26 00:44:59 -08:00
|
|
|
assert(GenericDataThing(3).count_plus_one == 4);
|
|
|
|
}
|
2017-01-22 16:51:37 -08:00
|
|
|
fn GenericDataThing(comptime count: isize) -> type {
|
2016-12-26 00:44:59 -08:00
|
|
|
struct {
|
|
|
|
const count_plus_one = count + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-16 13:02:35 -07:00
|
|
|
test "useGenericParamInGenericParam" {
|
2016-12-26 00:44:59 -08:00
|
|
|
assert(aGenericFn(i32, 3, 4) == 7);
|
|
|
|
}
|
2017-01-22 16:51:37 -08:00
|
|
|
fn aGenericFn(comptime T: type, comptime a: T, b: T) -> T {
|
2016-12-26 00:44:59 -08:00
|
|
|
return a + b;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-16 13:02:35 -07:00
|
|
|
test "genericFnWithImplicitCast" {
|
2016-12-27 22:35:13 -08:00
|
|
|
assert(getFirstByte(u8, []u8 {13}) == 13);
|
|
|
|
assert(getFirstByte(u16, []u16 {0, 13}) == 0);
|
|
|
|
}
|
2017-02-12 14:22:35 -08:00
|
|
|
fn getByte(ptr: ?&const u8) -> u8 {*??ptr}
|
|
|
|
fn getFirstByte(comptime T: type, mem: []const T) -> u8 {
|
2017-04-21 07:39:13 -07:00
|
|
|
getByte(@ptrCast(&const u8, &mem[0]))
|
2016-12-27 22:35:13 -08:00
|
|
|
}
|