047_methods.zig 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //
  2. // Help! Evil alien creatures have hidden eggs all over the Earth
  3. // and they're starting to hatch!
  4. //
  5. // Before you jump into battle, you'll need to know four things:
  6. //
  7. // 1. You can attach functions to structs:
  8. //
  9. // const Foo = struct{
  10. // pub fn hello() void {
  11. // std.debug.print("Foo says hello!\n", .{});
  12. // }
  13. // }
  14. //
  15. // 2. A function that is a member of a struct is a "method" and is
  16. // called with the "dot syntax" like so:
  17. //
  18. // Foo.hello();
  19. //
  20. // 3. The NEAT feature of methods is the special parameter named
  21. // "self" that takes an instance of that type of struct:
  22. //
  23. // const Bar = struct{
  24. // number: u32,
  25. //
  26. // pub fn printMe(self: *Bar) void {
  27. // std.debug.print("{}\n", .{self.number});
  28. // }
  29. // }
  30. //
  31. // (Actually, you can name the first parameter anything, but
  32. // please follow convention and use "self".)
  33. //
  34. // 4. Now when you call the method on an INSTANCE of that struct
  35. // with the "dot syntax", the instance will be automatically
  36. // passed as the "self" parameter:
  37. //
  38. // var my_bar = Bar{ .number = 2000 };
  39. // my_bar.printMe(); // prints "2000"
  40. //
  41. // Okay, you're armed.
  42. //
  43. // Now, please zap the alien structs until they're all gone or
  44. // Earth will be doomed!
  45. //
  46. const std = @import("std");
  47. // Look at this hideous Alien struct. Know your enemy!
  48. const Alien = struct {
  49. health: u8,
  50. // We hate this method:
  51. pub fn hatch(strength: u8) Alien {
  52. return Alien{
  53. .health = strength * 5,
  54. };
  55. }
  56. // We love this method:
  57. pub fn zap(self: *Alien, damage: u8) void {
  58. self.health -= if (damage >= self.health) self.health else damage;
  59. }
  60. };
  61. pub fn main() void {
  62. // Look at all of these aliens of various strengths!
  63. var aliens = [_]Alien{
  64. Alien.hatch(2),
  65. Alien.hatch(1),
  66. Alien.hatch(3),
  67. Alien.hatch(3),
  68. Alien.hatch(5),
  69. Alien.hatch(3),
  70. };
  71. var aliens_alive = aliens.len;
  72. var heat_ray_strength: u8 = 7; // We've been given a heat ray weapon.
  73. // We'll keep checking to see if we've killed all the aliens yet.
  74. while (aliens_alive > 0) {
  75. aliens_alive = 0;
  76. // Loop through every alien by reference by specifying that the capture value is a pointer
  77. for (aliens) |*alien| {
  78. // *** Zap the Alien Here! ***
  79. ???.zap(heat_ray_strength);
  80. // If the alien's health is still above 0, it's still alive.
  81. if (alien.health > 0) aliens_alive += 1;
  82. }
  83. std.debug.print("{} aliens. ", .{aliens_alive});
  84. }
  85. std.debug.print("Earth is saved!\n", .{});
  86. }