zig/test/cases/this.zig
Andrew Kelley b581da41f8 remove compiler directives
* add `setFnTest`, `setFnVisible`, `setFnStaticEval`,
   `setFnNoInline` builtin functions to replace previous
   directive functionality
 * add `coldcc` and `nakedcc` as keywords which can be used as part
   of a function prototype.
 * `setDebugSafety` builtin can be used to set debug safety features
   at a per block scope level.
 * closes #169
2016-09-28 02:33:32 -04:00

51 lines
779 B
Zig

const assert = @import("std").debug.assert;
const module = this;
struct Point(inline T: type) {
const Self = this;
x: T,
y: T,
fn addOne(self: &Self) {
self.x += 1;
self.y += 1;
}
}
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() {
@setFnTest(this, true);
assert(module.add(1, 2) == 3);
}
fn thisReferToContainer() {
@setFnTest(this, true);
var pt = Point(i32) {
.x = 12,
.y = 34,
};
pt.addOne();
assert(pt.x == 13);
assert(pt.y == 35);
}
fn thisReferToFn() {
@setFnTest(this, true);
assert(factorial(5) == 120);
}