mirror of
https://github.com/ziglang/zig.git
synced 2025-12-27 16:43:07 +00:00
* 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
51 lines
779 B
Zig
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);
|
|
}
|