summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Boesch <chrboesch@noreply.codeberg.org>2026-06-21 16:16:31 +0200
committerChris Boesch <chrboesch@noreply.codeberg.org>2026-06-21 16:16:31 +0200
commitcbcb570248a5297a0644fc22357bf81aac54545a (patch)
treeeb0e8ab53bfd9b8d65d24337c4193a48b88823c3
parentf3df08d8e05dc0543137ae9866b389df178b97c6 (diff)
expanded explanation about self vs *self
-rw-r--r--exercises/047_methods.zig18
-rw-r--r--patches/patches/047_methods.patch6
2 files changed, 21 insertions, 3 deletions
diff --git a/exercises/047_methods.zig b/exercises/047_methods.zig
index 3221ca2..3e88719 100644
--- a/exercises/047_methods.zig
+++ b/exercises/047_methods.zig
@@ -37,6 +37,24 @@
// self, others use a lowercase version of the type name, but feel
// free to use whatever is most appropriate.
//
+// "But hold on," you say, eyeing a() and b() suspiciously, "why
+// does one take 'self' and another take '*self'?" Sharp eye!
+//
+// It all comes down to a single question: does the function need
+// to CHANGE the struct?
+//
+// * Needs to change it? Take a pointer (*Bar). Without it you'd
+// be scribbling on a COPY, and your changes would evaporate the
+// instant the function returns. Poof.
+// * Only reads it? Plain Bar is just fine.
+// (For a big, bulky struct you might still write *const Bar to
+// avoid copying it around, but for small ones a copy is cheap.)
+//
+// You'll see this below: zap() takes 'self: HeatRay' by value
+// because it only reads the ray's damage, but it takes the alien
+// as '*Alien' because zapping is supposed to HURT - and that means
+// changing the alien's health for real, not on a throwaway copy.
+//
// Okay, you're armed.
//
// Now, please zap the alien structs until they're all gone or
diff --git a/patches/patches/047_methods.patch b/patches/patches/047_methods.patch
index a7e8a26..4755ecb 100644
--- a/patches/patches/047_methods.patch
+++ b/patches/patches/047_methods.patch
@@ -1,6 +1,6 @@
---- exercises/047_methods.zig 2023-10-03 22:15:22.122241138 +0200
-+++ answers/047_methods.zig 2023-10-05 20:04:07.056100214 +0200
-@@ -88,7 +88,7 @@
+--- exercises/047_methods.zig 2026-06-21 16:11:59.110876971 +0200
++++ answers/047_methods.zig 2026-06-21 16:13:07.066363558 +0200
+@@ -106,7 +106,7 @@
for (&aliens) |*alien| {
// *** Zap the alien with the heat ray here! ***