From f42e1fdb6672446f472fb16dfbaea14a10898bc0 Mon Sep 17 00:00:00 2001 From: Quentin Li Date: Sun, 2 Jan 2022 23:33:19 -0500 Subject: [PATCH 1/4] Presented examples for Scala 2 if syntax --- .../scala3-book/taste-control-structures.md | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/_overviews/scala3-book/taste-control-structures.md b/_overviews/scala3-book/taste-control-structures.md index 520453481f..9e0ef3f134 100644 --- a/_overviews/scala3-book/taste-control-structures.md +++ b/_overviews/scala3-book/taste-control-structures.md @@ -45,7 +45,16 @@ As you’ll see throughout this book, _all_ Scala control structures can be used > An expression returns a result, while a statement does not. > Statements are typically used for their side-effects, such as using `println` to print to the console. - +Of note, the `if`/`else` control struture in Scala 2 was constructed differently, with parentheses required and curly brackets instead of the keyword `then`. + +```if (test1) { + doX() +} else if (test2) { + doY() +} else { + doZ() +} +``` ## `for` loops and expressions @@ -60,12 +69,14 @@ for i <- ints do println(i) The code `i <- ints` is referred to as a _generator_, and the code that follows the `do` keyword is the _body_ of the loop. -The old syntax for this control structure was: +The old syntax in Scala 2 for this control structure was: ```scala for (i <- ints) println(i) ``` +Again, note the usage of parentheses and the new `for`-`do` in Scala 3. + ### Guards You can also use one or more `if` expressions inside a `for` loop. From d0adf5f82bf9c727a4cacc7c89406c6b11f8e42b Mon Sep 17 00:00:00 2001 From: Quentin Li Date: Sun, 2 Jan 2022 23:33:19 -0500 Subject: [PATCH 2/4] Presented examples for Scala 2 if syntax --- _overviews/scala3-book/taste-control-structures.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/_overviews/scala3-book/taste-control-structures.md b/_overviews/scala3-book/taste-control-structures.md index 9e0ef3f134..55ea3eb333 100644 --- a/_overviews/scala3-book/taste-control-structures.md +++ b/_overviews/scala3-book/taste-control-structures.md @@ -45,9 +45,10 @@ As you’ll see throughout this book, _all_ Scala control structures can be used > An expression returns a result, while a statement does not. > Statements are typically used for their side-effects, such as using `println` to print to the console. -Of note, the `if`/`else` control struture in Scala 2 was constructed differently, with parentheses required and curly brackets instead of the keyword `then`. +The `if`/`else` control struture in Scala 2 was constructed differently, with parentheses required and curly brackets instead of the keyword `then`. -```if (test1) { +```scala +if (test1) { doX() } else if (test2) { doY() From cbc6d00889bfd4b8c73bb4303a0de7457a9dc06e Mon Sep 17 00:00:00 2001 From: Quentin Li Date: Mon, 3 Jan 2022 08:41:33 -0500 Subject: [PATCH 3/4] Specify Scala 2 in if snippet Co-authored-by: Julien Richard-Foy --- _overviews/scala3-book/taste-control-structures.md | 1 + 1 file changed, 1 insertion(+) diff --git a/_overviews/scala3-book/taste-control-structures.md b/_overviews/scala3-book/taste-control-structures.md index 55ea3eb333..99c17705fc 100644 --- a/_overviews/scala3-book/taste-control-structures.md +++ b/_overviews/scala3-book/taste-control-structures.md @@ -48,6 +48,7 @@ As you’ll see throughout this book, _all_ Scala control structures can be used The `if`/`else` control struture in Scala 2 was constructed differently, with parentheses required and curly brackets instead of the keyword `then`. ```scala +// Scala 2 syntax if (test1) { doX() } else if (test2) { From 4aade2ced1dbb971c71d1abc020686dae755047e Mon Sep 17 00:00:00 2001 From: Quentin Li Date: Mon, 3 Jan 2022 08:41:56 -0500 Subject: [PATCH 4/4] Specify Scala 2 in for statement Co-authored-by: Julien Richard-Foy --- _overviews/scala3-book/taste-control-structures.md | 1 + 1 file changed, 1 insertion(+) diff --git a/_overviews/scala3-book/taste-control-structures.md b/_overviews/scala3-book/taste-control-structures.md index 99c17705fc..b55e9433b3 100644 --- a/_overviews/scala3-book/taste-control-structures.md +++ b/_overviews/scala3-book/taste-control-structures.md @@ -74,6 +74,7 @@ The code `i <- ints` is referred to as a _generator_, and the code that follows The old syntax in Scala 2 for this control structure was: ```scala +// Scala 2 syntax for (i <- ints) println(i) ```