Guest User

Untitled

a guest
Aug 23rd, 2019
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.53 KB | None | 0 0
  1. const Foo = struct {
  2. bar: usize = 42,
  3.  
  4. fn modifySelf(self: Foo, new_bar: usize) void {
  5. self.bar = new_bar;
  6. }
  7.  
  8. fn returnModifiedSelf(self: Foo, new_bar: usize) Foo {
  9. return Foo{
  10. .bar = new_bar,
  11. };
  12. }
  13. };
  14.  
  15. pub fn main() void {
  16. var foo = Foo{};
  17.  
  18. // This line doesn't compile since parameters are immutable
  19. // foo.modifySelf(21);
  20.  
  21. // This works but is a little awkward IMO — Is this the idiomatic way in Zig?
  22. foo = foo.returnModifiedSelf(21);
  23. }
Advertisement
Add Comment
Please, Sign In to add comment