047_methods.zig 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. };
  57. // Your trusty weapon. Zap those aliens!
  58. const HeatRay = struct {
  59. damage: u8,
  60. // We love this method:
  61. pub fn zap(self: *HeatRay, alien: *Alien) void {
  62. alien.health -= if (self.damage >= alien.health) alien.health else self.damage;
  63. }
  64. };
  65. pub fn main() void {
  66. // Look at all of these aliens of various strengths!
  67. var aliens = [_]Alien{
  68. Alien.hatch(2),
  69. Alien.hatch(1),
  70. Alien.hatch(3),
  71. Alien.hatch(3),
  72. Alien.hatch(5),
  73. Alien.hatch(3),
  74. };
  75. var aliens_alive = aliens.len;
  76. var heat_ray = HeatRay{ .damage = 7 }; // We've been given a heat ray weapon.
  77. // We'll keep checking to see if we've killed all the aliens yet.
  78. while (aliens_alive > 0) {
  79. aliens_alive = 0;
  80. // Loop through every alien...
  81. for (aliens) |*alien| {
  82. // *** Zap the alien with the heat ray here! ***
  83. ???.zap(???);
  84. // If the alien's health is still above 0, it's still alive.
  85. if (alien.health > 0) aliens_alive += 1;
  86. }
  87. std.debug.print("{} aliens. ", .{aliens_alive});
  88. }
  89. std.debug.print("Earth is saved!\n", .{});
  90. }