zig/test/cases/this.zig
Andrew Kelley fcbb7426fa use * for pointer type instead of &
See #770

To help automatically translate code, see the
zig-fmt-pointer-reform-2 branch.

This will convert all & into *. Due to the syntax
ambiguity (which is why we are making this change),
even address-of & will turn into *, so you'll have
to manually fix thes instances. You will be guaranteed
to get compile errors for them - expected 'type', found 'foo'
2018-05-31 17:28:07 -04:00

44 lines
751 B
Zig

const assert = @import("std").debug.assert;
const module = this;
fn Point(comptime T: type) type {
return struct {
const Self = this;
x: T,
y: T,
fn addOne(self: *Self) void {
self.x += 1;
self.y += 1;
}
};
}
fn add(x: i32, y: i32) i32 {
return x + y;
}
fn factorial(x: i32) i32 {
const selfFn = this;
return if (x == 0) 1 else x * selfFn(x - 1);
}
test "this refer to module call private fn" {
assert(module.add(1, 2) == 3);
}
test "this refer to container" {
var pt = Point(i32){
.x = 12,
.y = 34,
};
pt.addOne();
assert(pt.x == 13);
assert(pt.y == 35);
}
test "this refer to fn" {
assert(factorial(5) == 120);
}