From de353bcb3b6d7ef59bcf1ceb0a326ab6e5a4139f Mon Sep 17 00:00:00 2001 From: mikkurogue Date: Sat, 4 Jan 2025 11:56:25 +0000 Subject: [PATCH 1/5] Update: 108 Labeled switch example to contain default case for exhaustion Update example text to give clarity on default/exhaustive case. Reasoning: The input for the example will not compile if user would want to test this for the logic of a labeled switch. Due the input not being an exhaustive input but rather "any u8 integer" (for the lack of better terminology) we need to use the else branch to indicate that the default case is handled, in this case by just emulating the '4' branch, but this could return an error.InvalidCaseProvided for example. Signed-off-by: mikkurogue --- exercises/108_labeled_switch.zig | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/exercises/108_labeled_switch.zig b/exercises/108_labeled_switch.zig index 88cb196..d506cac 100644 --- a/exercises/108_labeled_switch.zig +++ b/exercises/108_labeled_switch.zig @@ -35,6 +35,7 @@ // 2 => continue :foo 3, // 3 => return, // 4 => {}, +// else => {}, // } // std.debug.print("This statement cannot be reached"); // } @@ -46,6 +47,9 @@ // 3. In the case '2' we repeat the same pattern as case '1' // but instead the value to be evaluated is now '3'; // 4. Finally we get to case '3', where we return from the function as a whole. +// 5. In this example as the input has no clear exhaustive patterns but a essentially +// any u8 integer, we need do need to handle any case that is not explicitly handled +// by using the `else => {}` branch as a default case. // // Since step 4 or a break stament do not exist in this switch, the debug statement is // never executed From 5cdaee74205e97da127d749b07cbe6e64ba6a940 Mon Sep 17 00:00:00 2001 From: mikkurogue Date: Tue, 7 Jan 2025 10:15:59 +0000 Subject: [PATCH 2/5] Update: Remove the 4th branch in favour of just making that the else branch Probably easier to not have an "unused" branch that returns the same as the else branch in the example. Signed-off-by: mikkurogue --- exercises/108_labeled_switch.zig | 1 - 1 file changed, 1 deletion(-) diff --git a/exercises/108_labeled_switch.zig b/exercises/108_labeled_switch.zig index d506cac..bd0b478 100644 --- a/exercises/108_labeled_switch.zig +++ b/exercises/108_labeled_switch.zig @@ -34,7 +34,6 @@ // 1 => continue :foo 2, // 2 => continue :foo 3, // 3 => return, -// 4 => {}, // else => {}, // } // std.debug.print("This statement cannot be reached"); From 87b258d0a90af4cf5e16986226b589c2a541db32 Mon Sep 17 00:00:00 2001 From: mikkurogue Date: Tue, 7 Jan 2025 18:00:16 +0000 Subject: [PATCH 3/5] Fix a typo: we need do we need to -> we need to Morning delirium does not help when looking at lots of words --- exercises/108_labeled_switch.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/108_labeled_switch.zig b/exercises/108_labeled_switch.zig index bd0b478..1e39935 100644 --- a/exercises/108_labeled_switch.zig +++ b/exercises/108_labeled_switch.zig @@ -47,7 +47,7 @@ // but instead the value to be evaluated is now '3'; // 4. Finally we get to case '3', where we return from the function as a whole. // 5. In this example as the input has no clear exhaustive patterns but a essentially -// any u8 integer, we need do need to handle any case that is not explicitly handled +// any u8 integer, we need to handle any case that is not explicitly handled // by using the `else => {}` branch as a default case. // // Since step 4 or a break stament do not exist in this switch, the debug statement is From 54f743ba43630a452f8104b5b21fbbcdeefe866f Mon Sep 17 00:00:00 2001 From: chrboesch Date: Wed, 8 Jan 2025 12:18:25 +0000 Subject: [PATCH 4/5] Update exercises/108_labeled_switch.zig The sentence was slightly unclear Signed-off-by: chrboesch --- exercises/108_labeled_switch.zig | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/exercises/108_labeled_switch.zig b/exercises/108_labeled_switch.zig index 1e39935..24cf2fa 100644 --- a/exercises/108_labeled_switch.zig +++ b/exercises/108_labeled_switch.zig @@ -46,9 +46,10 @@ // 3. In the case '2' we repeat the same pattern as case '1' // but instead the value to be evaluated is now '3'; // 4. Finally we get to case '3', where we return from the function as a whole. -// 5. In this example as the input has no clear exhaustive patterns but a essentially -// any u8 integer, we need to handle any case that is not explicitly handled -// by using the `else => {}` branch as a default case. +// 5. In this example, since the input does not have clear, exhaustive patterns and +// can essentially be any `u8` integer, we need to handle all cases not explicitly +// covered by using the `else => {}` branch as the default case. + // // Since step 4 or a break stament do not exist in this switch, the debug statement is // never executed From 90d05995b5d5453bfed4eef2547dd31efa3af412 Mon Sep 17 00:00:00 2001 From: chrboesch Date: Wed, 8 Jan 2025 13:22:29 +0000 Subject: [PATCH 5/5] Update exercises/108_labeled_switch.zig Fixed an error in the debug statement and made the text a bit more coherent. Signed-off-by: chrboesch --- exercises/108_labeled_switch.zig | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/exercises/108_labeled_switch.zig b/exercises/108_labeled_switch.zig index 24cf2fa..16a5879 100644 --- a/exercises/108_labeled_switch.zig +++ b/exercises/108_labeled_switch.zig @@ -15,11 +15,11 @@ // 1 => { op = 2; continue; }, // 2 => { op = 3; continue; }, // 3 => return, -// 4 => {}, +// else => {}, // } // break; // } -// std.debug.print("This statement cannot be reached"); +// std.debug.print("This statement cannot be reached\n", .{}); // } // // By combining all we've learned so far, we can now proceed with a labeled switch @@ -36,23 +36,21 @@ // 3 => return, // else => {}, // } -// std.debug.print("This statement cannot be reached"); +// std.debug.print("This statement cannot be reached\n", .{}); // } // // The flow of execution on this second case is: // 1. The switch starts with value '1'; // 2. The switch evaluates to case '1' which in turn uses the continue statement -// to re-evaluate the labeled switch again, now providing the value '2'; +// to re-evaluate the labeled switch again, now providing the value '2'; // 3. In the case '2' we repeat the same pattern as case '1' -// but instead the value to be evaluated is now '3'; -// 4. Finally we get to case '3', where we return from the function as a whole. +// but instead the value to be evaluated is now '3'; +// 4. Finally we get to case '3', where we return from the function as a whole, +// so the debug statement is never executed. // 5. In this example, since the input does not have clear, exhaustive patterns and -// can essentially be any `u8` integer, we need to handle all cases not explicitly -// covered by using the `else => {}` branch as the default case. - +// can essentially be any 'u8' integer, we need to handle all cases not explicitly +// covered by using the 'else => {}' branch as the default case. // -// Since step 4 or a break stament do not exist in this switch, the debug statement is -// never executed // const std = @import("std");