2017-01-05 00:57:48 -08:00
|
|
|
const assert = @import("std").debug.assert;
|
|
|
|
|
2016-09-26 20:47:30 -07:00
|
|
|
const module = this;
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn Point(comptime T: type) type {
|
2017-12-21 21:50:30 -08:00
|
|
|
return struct {
|
2016-12-21 22:20:08 -08:00
|
|
|
const Self = this;
|
|
|
|
x: T,
|
|
|
|
y: T,
|
2016-09-26 20:47:30 -07:00
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
fn addOne(self: *Self) void {
|
2016-12-21 22:20:08 -08:00
|
|
|
self.x += 1;
|
|
|
|
self.y += 1;
|
|
|
|
}
|
2017-12-21 21:50:30 -08:00
|
|
|
};
|
2016-09-26 20:47:30 -07:00
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn add(x: i32, y: i32) i32 {
|
2017-12-21 21:50:30 -08:00
|
|
|
return x + y;
|
2016-09-26 20:47:30 -07:00
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn factorial(x: i32) i32 {
|
2016-09-26 20:47:30 -07:00
|
|
|
const selfFn = this;
|
2017-12-21 21:50:30 -08:00
|
|
|
return if (x == 0) 1 else x * selfFn(x - 1);
|
2016-09-26 20:47:30 -07:00
|
|
|
}
|
|
|
|
|
2017-05-23 18:38:31 -07:00
|
|
|
test "this refer to module call private fn" {
|
2016-09-26 20:47:30 -07:00
|
|
|
assert(module.add(1, 2) == 3);
|
|
|
|
}
|
|
|
|
|
2017-05-23 18:38:31 -07:00
|
|
|
test "this refer to container" {
|
2018-05-28 17:23:55 -07:00
|
|
|
var pt = Point(i32){
|
2016-09-26 20:47:30 -07:00
|
|
|
.x = 12,
|
|
|
|
.y = 34,
|
|
|
|
};
|
|
|
|
pt.addOne();
|
|
|
|
assert(pt.x == 13);
|
|
|
|
assert(pt.y == 35);
|
|
|
|
}
|
|
|
|
|
2017-05-23 18:38:31 -07:00
|
|
|
test "this refer to fn" {
|
2016-09-26 20:47:30 -07:00
|
|
|
assert(factorial(5) == 120);
|
|
|
|
}
|