2016-09-26 20:47:30 -07:00
|
|
|
const module = this;
|
|
|
|
|
2016-12-21 22:20:08 -08:00
|
|
|
fn Point(inline T: type) -> type {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn thisReferToModuleCallPrivateFn() {
|
2016-12-21 22:20:08 -08:00
|
|
|
@setFnTest(this);
|
2016-09-27 23:33:32 -07:00
|
|
|
|
2016-09-26 20:47:30 -07:00
|
|
|
assert(module.add(1, 2) == 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn thisReferToContainer() {
|
2016-12-21 22:20:08 -08:00
|
|
|
@setFnTest(this);
|
2016-09-27 23:33:32 -07:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn thisReferToFn() {
|
2016-12-21 22:20:08 -08:00
|
|
|
@setFnTest(this);
|
2016-09-27 23:33:32 -07:00
|
|
|
|
2016-09-26 20:47:30 -07:00
|
|
|
assert(factorial(5) == 120);
|
|
|
|
}
|
2016-12-21 22:20:08 -08:00
|
|
|
|
|
|
|
// TODO const assert = @import("std").debug.assert;
|
|
|
|
fn assert(ok: bool) {
|
|
|
|
if (!ok)
|
|
|
|
@unreachable();
|
|
|
|
}
|