Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
33
Task/Call-an-object-method/Zig/call-an-object-method.zig
Normal file
33
Task/Call-an-object-method/Zig/call-an-object-method.zig
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
const assert = @import("std").debug.assert;
|
||||
|
||||
pub const ID = struct {
|
||||
name: []const u8,
|
||||
age: u7,
|
||||
|
||||
const Self = @This();
|
||||
|
||||
pub fn init(name: []const u8, age: u7) Self {
|
||||
return Self{
|
||||
.name = name,
|
||||
.age = age,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn getAge(self: Self) u7 {
|
||||
return self.age;
|
||||
}
|
||||
};
|
||||
|
||||
test "call an object method" {
|
||||
// Declare an instance of a struct by using a struct method.
|
||||
const person1 = ID.init("Alice", 18);
|
||||
|
||||
// Or by declaring it manually.
|
||||
const person2 = ID{
|
||||
.name = "Bob",
|
||||
.age = 20,
|
||||
};
|
||||
|
||||
assert(person1.getAge() == 18);
|
||||
assert(ID.getAge(person2) == 20);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue