Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const Foo = struct {
- bar: usize = 42,
- fn modifySelf(self: Foo, new_bar: usize) void {
- self.bar = new_bar;
- }
- fn returnModifiedSelf(self: Foo, new_bar: usize) Foo {
- return Foo{
- .bar = new_bar,
- };
- }
- };
- pub fn main() void {
- var foo = Foo{};
- // This line doesn't compile since parameters are immutable
- // foo.modifySelf(21);
- // This works but is a little awkward IMO — Is this the idiomatic way in Zig?
- foo = foo.returnModifiedSelf(21);
- }
Advertisement
Add Comment
Please, Sign In to add comment