From 57d7985015318b071a0be6bdf0965e3fbdeb978f Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Mon, 20 Dec 2021 15:18:44 +0800 Subject: [PATCH] Expand switch example. --- scripts/for1.rhai | 2 +- scripts/switch.rhai | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/scripts/for1.rhai b/scripts/for1.rhai index 1e215da5..4818b202 100644 --- a/scripts/for1.rhai +++ b/scripts/for1.rhai @@ -11,7 +11,7 @@ for (a, i) in arr { if a == 3 { break; } } -//print(a); // <- if you uncomment this line, the script will fail to run +//print(a); // <- if you uncomment this line, the script will fail to compile // because 'a' is not defined here for i in range(5, 0, -1) { // runs from 5 down to 1 diff --git a/scripts/switch.rhai b/scripts/switch.rhai index a252fa66..971ff989 100644 --- a/scripts/switch.rhai +++ b/scripts/switch.rhai @@ -1,11 +1,13 @@ -let arr = [42, 123.456, "hello", true, 'x', 999, 1]; +let arr = [42, 123.456, "hello", true, "hey", 'x', 999, 1, 2, 3, 4]; for item in arr { switch item { 42 => print("The Answer!"), 123.456 => print(`Floating point... ${item}`), "hello" => print(`${item} world!`), - 999 => print(`A number: ${item}`), - _ => print(`Something else: <${item}>`) + 999 => print(`Got 999: ${item}`), + 0..100 if item % 2 == 0 => print(`A small even number: ${item}`), + 0..100 => print(`A small odd number: ${item}`), + _ => print(`Something else: <${item}> is ${type_of(item)}`) } }