047_methods.zig 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 three things:
  6. //
  7. // 1. You can attach functions to structs (and other "type definitions"):
  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 "namespaced" within
  16. // that struct and is called by specifying the "namespace" and then
  17. // using the "dot syntax":
  18. //
  19. // Foo.hello();
  20. //
  21. // 3. The NEAT feature of these functions is that if their first argument
  22. // is an instance of the struct (or a pointer to one) then we can use
  23. // the instance as the namespace instead of the type:
  24. //
  25. // const Bar = struct{
  26. // pub fn a(self: Bar) void {}
  27. // pub fn b(this: *Bar, other: u8) void {}
  28. // pub fn c(bar: *const Bar) void {}
  29. // };
  30. //
  31. // var bar = Bar{};
  32. // bar.a() // is equivalent to Bar.a(bar)
  33. // bar.b(3) // is equivalent to Bar.b(&bar, 3)
  34. // bar.c() // is equivalent to Bar.c(&bar)
  35. //
  36. // Notice that the name of the parameter doesn't matter. Some use
  37. // self, others use a lowercase version of the type name, but feel
  38. // free to use whatever is most appropriate.
  39. //
  40. // Okay, you're armed.
  41. //
  42. // Now, please zap the alien structs until they're all gone or
  43. // Earth will be doomed!
  44. //
  45. const std = @import("std");
  46. // Look at this hideous Alien struct. Know your enemy!
  47. const Alien = struct {
  48. health: u8,
  49. // We hate this method:
  50. pub fn hatch(strength: u8) Alien {
  51. return Alien{
  52. .health = strength * 5,
  53. };
  54. }
  55. };
  56. // Your trusty weapon. Zap those aliens!
  57. const HeatRay = struct {
  58. damage: u8,
  59. // We love this method:
  60. pub fn zap(self: HeatRay, alien: *Alien) void {
  61. alien.health -= if (self.damage >= alien.health) alien.health else self.damage;
  62. }
  63. };
  64. pub fn main() void {
  65. // Look at all of these aliens of various strengths!
  66. var aliens = [_]Alien{
  67. Alien.hatch(2),
  68. Alien.hatch(1),
  69. Alien.hatch(3),
  70. Alien.hatch(3),
  71. Alien.hatch(5),
  72. Alien.hatch(3),
  73. };
  74. var aliens_alive = aliens.len;
  75. var heat_ray = HeatRay{ .damage = 7 }; // We've been given a heat ray weapon.
  76. // We'll keep checking to see if we've killed all the aliens yet.
  77. while (aliens_alive > 0) {
  78. aliens_alive = 0;
  79. // Loop through every alien by reference (* makes a pointer capture value)
  80. for (&aliens) |*alien| {
  81. // *** Zap the alien with the heat ray here! ***
  82. ???.zap(???);
  83. // If the alien's health is still above 0, it's still alive.
  84. if (alien.health > 0) aliens_alive += 1;
  85. }
  86. std.debug.print("{} aliens. ", .{aliens_alive});
  87. }
  88. std.debug.print("Earth is saved!\n", .{});
  89. }