Call an object method: Difference between revisions

Content added Content deleted
imported>Acediast
(→‎{{header|COBOL}}: switched to free format)
m (→‎{{header|Zig}}: update to use Zig's testing)
Line 2,345: Line 2,345:
Zig does not have classes nor objects. Zig's structs, however, can have methods; but they are not special. They are only namespaced functions that can be called with dot syntax.
Zig does not have classes nor objects. Zig's structs, however, can have methods; but they are not special. They are only namespaced functions that can be called with dot syntax.


<syntaxhighlight lang="zig">const assert = @import("std").debug.assert;
<syntaxhighlight lang="zig">const testing = @import("std").testing;


pub const ID = struct {
pub const ID = struct {
Line 2,375: Line 2,375:
};
};


assert(person1.getAge() == 18);
// test getAge() method call
assert(ID.getAge(person2) == 20);
try testing.expectEqual(@as(u7, 18), person1.getAge());
try testing.expectEqual(@as(u7, 20), ID.getAge(person2));
}</syntaxhighlight>
}</syntaxhighlight>

=={{header|zkl}}==
=={{header|zkl}}==
In zkl, a class can be static (there will only exist one instance of the class). A function is always a member of some class but will only be static if it does not refer to instance data.
In zkl, a class can be static (there will only exist one instance of the class). A function is always a member of some class but will only be static if it does not refer to instance data.