From 7679f93f688452b2236d20b49f92569b8d56e20d Mon Sep 17 00:00:00 2001 From: Chris Boesch Date: Tue, 21 Nov 2023 15:01:22 +0100 Subject: [PATCH 1/2] Converted var to const if there is no mutation in var. This is checked from compiler version 0.12.0-dev.1664 --- exercises/025_errors5.zig | 2 +- exercises/029_errdefer.zig | 4 ++-- exercises/039_pointers.zig | 2 +- exercises/048_methods2.zig | 2 +- exercises/049_quiz6.zig | 2 +- exercises/055_unions.zig | 4 ++-- exercises/056_unions2.zig | 4 ++-- exercises/057_unions3.zig | 4 ++-- exercises/058_quiz7.zig | 4 ++-- exercises/067_comptime2.zig | 8 ++++---- exercises/070_comptime5.zig | 6 +++--- exercises/072_comptime7.zig | 2 +- exercises/075_quiz8.zig | 4 ++-- exercises/076_sentinels.zig | 2 +- exercises/080_anonymous_structs.zig | 4 ++-- exercises/092_interfaces.zig | 2 +- exercises/096_memory_allocation.zig | 4 ++-- patches/patches/025_errors5.patch | 8 ++++---- patches/patches/075_quiz8.patch | 4 ++-- patches/patches/080_anonymous_structs.patch | 12 ++++++------ patches/patches/096_memory_allocation.patch | 8 ++++---- 21 files changed, 46 insertions(+), 46 deletions(-) diff --git a/exercises/025_errors5.zig b/exercises/025_errors5.zig index 63595b1..94bf1c7 100644 --- a/exercises/025_errors5.zig +++ b/exercises/025_errors5.zig @@ -26,7 +26,7 @@ fn addFive(n: u32) MyNumberError!u32 { // This function needs to return any error which might come back from detect(). // Please use a "try" statement rather than a "catch". // - var x = detect(n); + const x = detect(n); return x + 5; } diff --git a/exercises/029_errdefer.zig b/exercises/029_errdefer.zig index 82fdfe1..39ab306 100644 --- a/exercises/029_errdefer.zig +++ b/exercises/029_errdefer.zig @@ -21,8 +21,8 @@ const MyErr = error{ GetFail, IncFail }; pub fn main() void { // We simply quit the entire program if we fail to get a number: - var a: u32 = makeNumber() catch return; - var b: u32 = makeNumber() catch return; + const a: u32 = makeNumber() catch return; + const b: u32 = makeNumber() catch return; std.debug.print("Numbers: {}, {}\n", .{ a, b }); } diff --git a/exercises/039_pointers.zig b/exercises/039_pointers.zig index d545525..24ca46d 100644 --- a/exercises/039_pointers.zig +++ b/exercises/039_pointers.zig @@ -24,7 +24,7 @@ const std = @import("std"); pub fn main() void { var num1: u8 = 5; - var num1_pointer: *u8 = &num1; + const num1_pointer: *u8 = &num1; var num2: u8 = undefined; diff --git a/exercises/048_methods2.zig b/exercises/048_methods2.zig index 3291965..a1fbe9e 100644 --- a/exercises/048_methods2.zig +++ b/exercises/048_methods2.zig @@ -24,7 +24,7 @@ const Elephant = struct { pub fn print(self: *Elephant) void { // Prints elephant letter and [v]isited - var v: u8 = if (self.visited) 'v' else ' '; + const v: u8 = if (self.visited) 'v' else ' '; std.debug.print("{u}{u} ", .{ self.letter, v }); } }; diff --git a/exercises/049_quiz6.zig b/exercises/049_quiz6.zig index 92541a5..9dbea51 100644 --- a/exercises/049_quiz6.zig +++ b/exercises/049_quiz6.zig @@ -37,7 +37,7 @@ const Elephant = struct { pub fn print(self: *Elephant) void { // Prints elephant letter and [v]isited - var v: u8 = if (self.visited) 'v' else ' '; + const v: u8 = if (self.visited) 'v' else ' '; std.debug.print("{u}{u} ", .{ self.letter, v }); } }; diff --git a/exercises/055_unions.zig b/exercises/055_unions.zig index 6339fc8..794f2df 100644 --- a/exercises/055_unions.zig +++ b/exercises/055_unions.zig @@ -53,8 +53,8 @@ const AntOrBee = enum { a, b }; pub fn main() void { // We'll just make one bee and one ant to test them out: - var ant = Insect{ .still_alive = true }; - var bee = Insect{ .flowers_visited = 15 }; + const ant = Insect{ .still_alive = true }; + const bee = Insect{ .flowers_visited = 15 }; std.debug.print("Insect report! ", .{}); diff --git a/exercises/056_unions2.zig b/exercises/056_unions2.zig index e4294db..c46d133 100644 --- a/exercises/056_unions2.zig +++ b/exercises/056_unions2.zig @@ -38,8 +38,8 @@ const Insect = union(InsectStat) { }; pub fn main() void { - var ant = Insect{ .still_alive = true }; - var bee = Insect{ .flowers_visited = 16 }; + const ant = Insect{ .still_alive = true }; + const bee = Insect{ .flowers_visited = 16 }; std.debug.print("Insect report! ", .{}); diff --git a/exercises/057_unions3.zig b/exercises/057_unions3.zig index 142180f..a5017d2 100644 --- a/exercises/057_unions3.zig +++ b/exercises/057_unions3.zig @@ -21,8 +21,8 @@ const Insect = union(InsectStat) { }; pub fn main() void { - var ant = Insect{ .still_alive = true }; - var bee = Insect{ .flowers_visited = 17 }; + const ant = Insect{ .still_alive = true }; + const bee = Insect{ .flowers_visited = 17 }; std.debug.print("Insect report! ", .{}); diff --git a/exercises/058_quiz7.zig b/exercises/058_quiz7.zig index bbdebf6..cf32fc3 100644 --- a/exercises/058_quiz7.zig +++ b/exercises/058_quiz7.zig @@ -273,7 +273,7 @@ const HermitsNotebook = struct { // distance) than the one we'd noted before. If it is, we // overwrite the old entry with the new one. fn checkNote(self: *HermitsNotebook, note: NotebookEntry) void { - var existing_entry = self.getEntry(note.place); + const existing_entry = self.getEntry(note.place); if (existing_entry == null) { self.entries[self.end_of_entries] = note; @@ -386,7 +386,7 @@ pub fn main() void { // "start" entry we just added) until we run out, at which point // we'll have checked every reachable Place. while (notebook.hasNextEntry()) { - var place_entry = notebook.getNextEntry(); + const place_entry = notebook.getNextEntry(); // For every Path that leads FROM the current Place, create a // new note (in the form of a NotebookEntry) with the diff --git a/exercises/067_comptime2.zig b/exercises/067_comptime2.zig index 7d4bdb0..6b9b14a 100644 --- a/exercises/067_comptime2.zig +++ b/exercises/067_comptime2.zig @@ -38,16 +38,16 @@ pub fn main() void { var count = 0; count += 1; - var a1: [count]u8 = .{'A'} ** count; + const a1: [count]u8 = .{'A'} ** count; count += 1; - var a2: [count]u8 = .{'B'} ** count; + const a2: [count]u8 = .{'B'} ** count; count += 1; - var a3: [count]u8 = .{'C'} ** count; + const a3: [count]u8 = .{'C'} ** count; count += 1; - var a4: [count]u8 = .{'D'} ** count; + const a4: [count]u8 = .{'D'} ** count; print("{s} {s} {s} {s}\n", .{ a1, a2, a3, a4 }); diff --git a/exercises/070_comptime5.zig b/exercises/070_comptime5.zig index 3b85aae..afd4ae8 100644 --- a/exercises/070_comptime5.zig +++ b/exercises/070_comptime5.zig @@ -83,19 +83,19 @@ const DuctError = error{UnmatchedDiameters}; pub fn main() void { // This is a real duck! - var ducky1 = Duck{ + const ducky1 = Duck{ .eggs = 0, .loudness = 3, }; // This is not a real duck, but it has quack() and waddle() // abilities, so it's still a "duck". - var ducky2 = RubberDuck{ + const ducky2 = RubberDuck{ .in_bath = false, }; // This is not even remotely a duck. - var ducky3 = Duct{ + const ducky3 = Duct{ .diameter = 17, .length = 165, .galvanized = true, diff --git a/exercises/072_comptime7.zig b/exercises/072_comptime7.zig index 9bc30fb..631e75b 100644 --- a/exercises/072_comptime7.zig +++ b/exercises/072_comptime7.zig @@ -39,7 +39,7 @@ pub fn main() void { // This gets the digit from the "instruction". Can you // figure out why we subtract '0' from it? - comptime var digit = instructions[i + 1] - '0'; + const digit = instructions[i + 1] - '0'; // This 'switch' statement contains the actual work done // at runtime. At first, this doesn't seem exciting... diff --git a/exercises/075_quiz8.zig b/exercises/075_quiz8.zig index 12b460c..63d208b 100644 --- a/exercises/075_quiz8.zig +++ b/exercises/075_quiz8.zig @@ -110,7 +110,7 @@ const HermitsNotebook = struct { } fn checkNote(self: *HermitsNotebook, note: NotebookEntry) void { - var existing_entry = self.getEntry(note.place); + const existing_entry = self.getEntry(note.place); if (existing_entry == null) { self.entries[self.end_of_entries] = note; @@ -180,7 +180,7 @@ pub fn main() void { notebook.checkNote(working_note); while (notebook.hasNextEntry()) { - var place_entry = notebook.getNextEntry(); + const place_entry = notebook.getNextEntry(); for (place_entry.place.paths) |*path| { working_note = NotebookEntry{ diff --git a/exercises/076_sentinels.zig b/exercises/076_sentinels.zig index 1af59da..4b1afb1 100644 --- a/exercises/076_sentinels.zig +++ b/exercises/076_sentinels.zig @@ -46,7 +46,7 @@ pub fn main() void { var nums = [_:0]u32{ 1, 2, 3, 4, 5, 6 }; // And here's a zero-terminated many-item pointer: - var ptr: [*:0]u32 = &nums; + const ptr: [*:0]u32 = &nums; // For fun, let's replace the value at position 3 with the // sentinel value 0. This seems kind of naughty. diff --git a/exercises/080_anonymous_structs.zig b/exercises/080_anonymous_structs.zig index 8205641..55dedd4 100644 --- a/exercises/080_anonymous_structs.zig +++ b/exercises/080_anonymous_structs.zig @@ -48,13 +48,13 @@ pub fn main() void { // * circle1 should hold i32 integers // * circle2 should hold f32 floats // - var circle1 = ??? { + const circle1 = ??? { .center_x = 25, .center_y = 70, .radius = 15, }; - var circle2 = ??? { + const circle2 = ??? { .center_x = 25.234, .center_y = 70.999, .radius = 15.714, diff --git a/exercises/092_interfaces.zig b/exercises/092_interfaces.zig index 7c958eb..7775dd5 100644 --- a/exercises/092_interfaces.zig +++ b/exercises/092_interfaces.zig @@ -96,7 +96,7 @@ const Insect = union(enum) { }; pub fn main() !void { - var my_insects = [_]Insect{ + const my_insects = [_]Insect{ Insect{ .ant = Ant{ .still_alive = true } }, Insect{ .bee = Bee{ .flowers_visited = 17 } }, Insect{ .grasshopper = Grasshopper{ .distance_hopped = 32 } }, diff --git a/exercises/096_memory_allocation.zig b/exercises/096_memory_allocation.zig index 6e818b5..1ece922 100644 --- a/exercises/096_memory_allocation.zig +++ b/exercises/096_memory_allocation.zig @@ -52,7 +52,7 @@ fn runningAverage(arr: []const f64, avg: []f64) void { pub fn main() !void { // pretend this was defined by reading in user input - var arr: []const f64 = &[_]f64{ 0.3, 0.2, 0.1, 0.1, 0.4 }; + const arr: []const f64 = &[_]f64{ 0.3, 0.2, 0.1, 0.1, 0.4 }; // initialize the allocator var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); @@ -64,7 +64,7 @@ pub fn main() !void { const allocator = arena.allocator(); // allocate memory for this array - var avg: []f64 = ???; + const avg: []f64 = ???; runningAverage(arr, avg); std.debug.print("Running Average: ", .{}); diff --git a/patches/patches/025_errors5.patch b/patches/patches/025_errors5.patch index 4495ed6..6dad689 100644 --- a/patches/patches/025_errors5.patch +++ b/patches/patches/025_errors5.patch @@ -1,11 +1,11 @@ ---- exercises/025_errors5.zig 2023-10-03 22:15:22.122241138 +0200 -+++ answers/025_errors5.zig 2023-10-05 20:04:06.952764946 +0200 +--- exercises/025_errors5.zig 2023-11-21 14:22:48.159250165 +0100 ++++ answers/025_errors5.zig 2023-11-21 14:25:01.338277886 +0100 @@ -26,7 +26,7 @@ // This function needs to return any error which might come back from detect(). // Please use a "try" statement rather than a "catch". // -- var x = detect(n); -+ var x = try detect(n); +- const x = detect(n); ++ const x = try detect(n); return x + 5; } diff --git a/patches/patches/075_quiz8.patch b/patches/patches/075_quiz8.patch index 1bb9e5d..1ff5016 100644 --- a/patches/patches/075_quiz8.patch +++ b/patches/patches/075_quiz8.patch @@ -1,5 +1,5 @@ ---- exercises/075_quiz8.zig 2023-10-03 22:15:22.125574535 +0200 -+++ answers/075_quiz8.zig 2023-10-05 20:04:07.182769252 +0200 +--- exercises/075_quiz8.zig 2023-11-21 14:48:15.440702720 +0100 ++++ answers/075_quiz8.zig 2023-11-21 14:50:23.453311616 +0100 @@ -49,7 +49,11 @@ // // Please fill in the body of this function! diff --git a/patches/patches/080_anonymous_structs.patch b/patches/patches/080_anonymous_structs.patch index a46ea41..52c0980 100644 --- a/patches/patches/080_anonymous_structs.patch +++ b/patches/patches/080_anonymous_structs.patch @@ -1,18 +1,18 @@ ---- exercises/080_anonymous_structs.zig 2023-10-03 22:15:22.125574535 +0200 -+++ answers/080_anonymous_structs.zig 2023-10-05 20:04:07.202769626 +0200 +--- exercises/080_anonymous_structs.zig 2023-11-21 14:52:54.312749682 +0100 ++++ answers/080_anonymous_structs.zig 2023-11-21 14:52:43.909225238 +0100 @@ -48,13 +48,13 @@ // * circle1 should hold i32 integers // * circle2 should hold f32 floats // -- var circle1 = ??? { -+ var circle1 = Circle(i32){ +- const circle1 = ??? { ++ const circle1 = Circle(i32){ .center_x = 25, .center_y = 70, .radius = 15, }; -- var circle2 = ??? { -+ var circle2 = Circle(f32){ +- const circle2 = ??? { ++ const circle2 = Circle(f32){ .center_x = 25.234, .center_y = 70.999, .radius = 15.714, diff --git a/patches/patches/096_memory_allocation.patch b/patches/patches/096_memory_allocation.patch index aab718f..c26eeeb 100644 --- a/patches/patches/096_memory_allocation.patch +++ b/patches/patches/096_memory_allocation.patch @@ -1,11 +1,11 @@ ---- exercises/096_memory_allocation.zig 2023-10-03 22:15:22.125574535 +0200 -+++ answers/096_memory_allocation.zig 2023-10-05 20:04:07.276104333 +0200 +--- exercises/096_memory_allocation.zig 2023-11-21 14:55:33.805678390 +0100 ++++ answers/096_memory_allocation.zig 2023-11-21 14:56:00.236163484 +0100 @@ -64,7 +64,7 @@ const allocator = arena.allocator(); // allocate memory for this array -- var avg: []f64 = ???; -+ var avg: []f64 = try allocator.alloc(f64, arr.len); +- const avg: []f64 = ???; ++ const avg: []f64 = try allocator.alloc(f64, arr.len); runningAverage(arr, avg); std.debug.print("Running Average: ", .{}); From b0511bb3c7ac0293edbb68fc54fbd8eba27a5882 Mon Sep 17 00:00:00 2001 From: Chris Boesch Date: Tue, 21 Nov 2023 15:22:36 +0100 Subject: [PATCH 2/2] Fixed patch hunk --- patches/patches/067_comptime2.patch | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/patches/patches/067_comptime2.patch b/patches/patches/067_comptime2.patch index 470c69f..adc6e64 100644 --- a/patches/patches/067_comptime2.patch +++ b/patches/patches/067_comptime2.patch @@ -1,5 +1,5 @@ ---- exercises/067_comptime2.zig 2023-10-03 22:15:22.125574535 +0200 -+++ answers/067_comptime2.zig 2023-10-05 20:04:07.146101899 +0200 +--- exercises/067_comptime2.zig 2023-11-21 14:36:12.080295365 +0100 ++++ answers/067_comptime2.zig 2023-11-21 15:11:50.814098876 +0100 @@ -35,7 +35,7 @@ // In this contrived example, we've decided to allocate some // arrays using a variable count! But something's missing... @@ -8,4 +8,4 @@ + comptime var count = 0; count += 1; - var a1: [count]u8 = .{'A'} ** count; + const a1: [count]u8 = .{'A'} ** count;