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;
|
|
|
|
|
2017-01-22 16:51:37 -08:00
|
|
|
fn Point(comptime T: type) -> type {
|
2016-12-21 22:20:08 -08:00
|
|
|
struct {
|
|
|
|
const Self = this;
|
|
|
|
x: T,
|
|
|
|
y: T,
|
2016-09-26 20:47:30 -07:00
|
|
|
|
2016-12-21 22:20:08 -08:00
|
|
|
fn addOne(self: &Self) {
|
|
|
|
self.x += 1;
|
|
|
|
self.y += 1;
|
|
|
|
}
|
2016-09-26 20:47:30 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add(x: i32, y: i32) -> i32 {
|
|
|
|
x + y
|
|
|
|
}
|
|
|
|
|
|
|
|
fn factorial(x: i32) -> i32 {
|
|
|
|
const selfFn = this;
|
|
|
|
if (x == 0) {
|
|
|
|
1
|
|
|
|
} else {
|
|
|
|
x * selfFn(x - 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-16 13:02:35 -07:00
|
|
|
test "thisReferToModuleCallPrivateFn" {
|
2016-09-26 20:47:30 -07:00
|
|
|
assert(module.add(1, 2) == 3);
|
|
|
|
}
|
|
|
|
|
2017-03-16 13:02:35 -07:00
|
|
|
test "thisReferToContainer" {
|
2016-09-26 20:47:30 -07:00
|
|
|
var pt = Point(i32) {
|
|
|
|
.x = 12,
|
|
|
|
.y = 34,
|
|
|
|
};
|
|
|
|
pt.addOne();
|
|
|
|
assert(pt.x == 13);
|
|
|
|
assert(pt.y == 35);
|
|
|
|
}
|
|
|
|
|
2017-03-16 13:02:35 -07:00
|
|
|
test "thisReferToFn" {
|
2016-09-26 20:47:30 -07:00
|
|
|
assert(factorial(5) == 120);
|
|
|
|
}
|