zig/test/cases/this.zig
Andrew Kelley 3671582c15 syntax: functions require return type. remove ->
The purpose of this is:

 * Only one way to do things
 * Changing a function with void return type to return a possible
   error becomes a 1 character change, subtly encouraging
   people to use errors.

See #632

Here are some imperfect sed commands for performing this update:

remove arrow:

```
sed -i 's/\(\bfn\b.*\)-> /\1/g' $(find . -name "*.zig")
```

add void:

```
sed -i 's/\(\bfn\b.*\))\s*{/\1) void {/g' $(find ../ -name "*.zig")
```

Some cleanup may be necessary, but this should do the bulk of the work.
2018-01-25 04:10:11 -05:00

44 lines
752 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);
}