047_methods.zig 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 they take either
  22. // an instance of the struct or a pointer to an instance of the struct
  23. // then they have some syntax sugar:
  24. //
  25. // const Bar = struct{
  26. // pub fn a(self: Bar) void { _ = self; }
  27. // pub fn b(this: *Bar, other: u8) void { _ = this; _ = other; }
  28. // pub fn c(bar: *const Bar) void { _ = bar; }
  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. // Effectively, the method syntax sugar just does this transformation:
  41. // thing.function(args);
  42. // @TypeOf(thing).function(thing, args);
  43. //
  44. // Okay, you're armed.
  45. //
  46. // Now, please zap the alien structs until they're all gone or
  47. // Earth will be doomed!
  48. //
  49. const std = @import("std");
  50. // Look at this hideous Alien struct. Know your enemy!
  51. const Alien = struct {
  52. health: u8,
  53. // We hate this method:
  54. pub fn hatch(strength: u8) Alien {
  55. return Alien{
  56. .health = strength * 5,
  57. };
  58. }
  59. };
  60. // Your trusty weapon. Zap those aliens!
  61. const HeatRay = struct {
  62. damage: u8,
  63. // We love this method:
  64. pub fn zap(self: HeatRay, alien: *Alien) void {
  65. alien.health -|= self.damage; // Saturating inplace substraction
  66. // It subtracts but doesn't go below the
  67. // lowest value for our type (in this case 0)
  68. }
  69. };
  70. pub fn main() void {
  71. // Look at all of these aliens of various strengths!
  72. var aliens = [_]Alien{
  73. Alien.hatch(2),
  74. Alien.hatch(1),
  75. Alien.hatch(3),
  76. Alien.hatch(3),
  77. Alien.hatch(5),
  78. Alien.hatch(3),
  79. };
  80. var aliens_alive = aliens.len;
  81. var heat_ray = HeatRay{ .damage = 7 }; // We've been given a heat ray weapon.
  82. // We'll keep checking to see if we've killed all the aliens yet.
  83. while (aliens_alive > 0) {
  84. aliens_alive = 0;
  85. // Loop through every alien by reference (* makes a pointer capture value)
  86. for (&aliens) |*alien| {
  87. // *** Zap the alien with the heat ray here! ***
  88. ???.zap(???);
  89. // If the alien's health is still above 0, it's still alive.
  90. if (alien.health > 0) aliens_alive += 1;
  91. }
  92. std.debug.print("{} aliens. ", .{aliens_alive});
  93. }
  94. std.debug.print("Earth is saved!\n", .{});
  95. }