diff --git a/code-samples/control-structures-conditionals-expression-implicit-none.pony b/code-samples/control-structures-conditionals-expression-implicit-none.pony index bb9d0837..b0afb94d 100644 --- a/code-samples/control-structures-conditionals-expression-implicit-none.pony +++ b/code-samples/control-structures-conditionals-expression-implicit-none.pony @@ -1,4 +1,8 @@ -var x: (String | None) = - if friendly then - "Hello" - end \ No newline at end of file +actor Main + new create(env: Env) => + let friendly = false + var x: (String | None) = + if friendly then + "Hello" + end + env.out.print(x.string()) \ No newline at end of file diff --git a/code-samples/control-structures-conditionals-expression-union-type.pony b/code-samples/control-structures-conditionals-expression-union-type.pony index ee2ea45a..91969306 100644 --- a/code-samples/control-structures-conditionals-expression-union-type.pony +++ b/code-samples/control-structures-conditionals-expression-union-type.pony @@ -1,6 +1,10 @@ -var x: (String | Bool) = - if friendly then - "Hello" - else - false - end \ No newline at end of file +actor Main + new create(env: Env) => + let friendly = false + var x: (String | Bool) = + if friendly then + "Hello" + else + false + end + env.out.print(x.string()) \ No newline at end of file diff --git a/code-samples/control-structures-conditionals-expressions.pony b/code-samples/control-structures-conditionals-expressions.pony index fb1ca3e1..b41f7c65 100644 --- a/code-samples/control-structures-conditionals-expressions.pony +++ b/code-samples/control-structures-conditionals-expressions.pony @@ -1 +1,6 @@ -x = 1 + if lots then 100 else 2 end \ No newline at end of file +actor Main + new create(env: Env) => + let lots = true + var x: U32 + x = 1 + if lots then 100 else 2 end + env.out.print("x = " + x.string() + "—that's " + (if lots then "lots" else "not a lot" end)) \ No newline at end of file diff --git a/code-samples/control-structures-conditionals-if-else.pony b/code-samples/control-structures-conditionals-if-else.pony index 14033612..2d61737c 100644 --- a/code-samples/control-structures-conditionals-if-else.pony +++ b/code-samples/control-structures-conditionals-if-else.pony @@ -1,5 +1,9 @@ -if a > b then - env.out.print("a is bigger") -else - env.out.print("a is not bigger") -end \ No newline at end of file +actor Main + new create(env: Env) => + let a: U8 = 1 + let b: U8 = 2 + if a > b then + env.out.print("a is bigger") + else + env.out.print("a is not bigger") + end \ No newline at end of file diff --git a/code-samples/control-structures-conditionals-if-elseif-else.pony b/code-samples/control-structures-conditionals-if-elseif-else.pony index f9d0a01f..824f8056 100644 --- a/code-samples/control-structures-conditionals-if-elseif-else.pony +++ b/code-samples/control-structures-conditionals-if-elseif-else.pony @@ -1,7 +1,11 @@ -if a == b then - env.out.print("they are the same") -elseif a > b then - env.out.print("a is bigger") -else - env.out.print("b bigger") -end \ No newline at end of file +actor Main + new create(env: Env) => + let a: U8 = 2 + let b: U8 = 1 + if a == b then + env.out.print("they are the same") + elseif a > b then + env.out.print("a is bigger") + else + env.out.print("b bigger") + end \ No newline at end of file diff --git a/code-samples/control-structures-conditionals-if.pony b/code-samples/control-structures-conditionals-if.pony index a2a672f2..7c006a4d 100644 --- a/code-samples/control-structures-conditionals-if.pony +++ b/code-samples/control-structures-conditionals-if.pony @@ -1,3 +1,7 @@ -if a > b then - env.out.print("a is bigger") -end \ No newline at end of file +actor Main + new create(env: Env) => + let a: U8 = 2 + let b: U8 = 1 + if a > b then + env.out.print("a is bigger") + end \ No newline at end of file diff --git a/code-samples/control-structures-conditionals-nested-if-else.pony b/code-samples/control-structures-conditionals-nested-if-else.pony index df7477bd..5f796257 100644 --- a/code-samples/control-structures-conditionals-nested-if-else.pony +++ b/code-samples/control-structures-conditionals-nested-if-else.pony @@ -1,9 +1,13 @@ -if a == b then - env.out.print("they are the same") -else - if a > b then - env.out.print("a is bigger") - else - env.out.print("b bigger") - end -end \ No newline at end of file +actor Main + new create(env: Env) => + let a: U8 = 2 + let b: U8 = 2 + if a == b then + env.out.print("they are the same") + else + if a > b then + env.out.print("a is bigger") + else + env.out.print("b bigger") + end + end \ No newline at end of file diff --git a/code-samples/control-structures-loops-for-while-comparison.pony b/code-samples/control-structures-loops-for-while-comparison.pony index 91d327cb..e8acf0a9 100644 --- a/code-samples/control-structures-loops-for-while-comparison.pony +++ b/code-samples/control-structures-loops-for-while-comparison.pony @@ -1,5 +1,9 @@ -let iterator = ["Bob"; "Fred"; "Sarah"].values() -while iterator.has_next() do - let name = iterator.next()? - env.out.print(name) -end \ No newline at end of file +actor Main + new create(env: Env) => + try + let iterator = ["Bob"; "Fred"; "Sarah"].values() + while iterator.has_next() do + let name = iterator.next()? + env.out.print(name) + end + end \ No newline at end of file diff --git a/code-samples/control-structures-loops-for.pony b/code-samples/control-structures-loops-for.pony index 64d97f3a..d8ee843d 100644 --- a/code-samples/control-structures-loops-for.pony +++ b/code-samples/control-structures-loops-for.pony @@ -1,3 +1,5 @@ -for name in ["Bob"; "Fred"; "Sarah"].values() do - env.out.print(name) -end \ No newline at end of file +actor Main + new create(env: Env) => + for name in ["Bob"; "Fred"; "Sarah"].values() do + env.out.print(name) + end \ No newline at end of file diff --git a/code-samples/control-structures-loops-while-break-else.pony b/code-samples/control-structures-loops-while-break-else.pony index 357cd5c0..32ab90d0 100644 --- a/code-samples/control-structures-loops-while-break-else.pony +++ b/code-samples/control-structures-loops-while-break-else.pony @@ -1,10 +1,31 @@ -var name = - while moreNames() do - var name' = getName() - if name' == "Jack" or name' == "Jill" then - break name' - end - name' - else +actor Main + let names: Array[String] = [ + "Jack" "Herbert" - end \ No newline at end of file + "Jill" + ] + var current_name: String = "" + + new create(env: Env) => + var name = + while moreNames() do + var name' = getName() + if (name' == "Jack") or (name' == "Jill") then + break name' + end + name' + else + "Herbert" + end + env.out.print("name = " + name) + + fun ref moreNames(): Bool => + try + current_name = names.shift()? + else + return false + end + true + + fun getName(): String => + current_name \ No newline at end of file diff --git a/code-samples/control-structures-loops-while.pony b/code-samples/control-structures-loops-while.pony index 881ef4b5..cfe99ab1 100644 --- a/code-samples/control-structures-loops-while.pony +++ b/code-samples/control-structures-loops-while.pony @@ -1,6 +1,8 @@ -var count: U32 = 1 +actor Main + new create(env: Env) => + var count: U32 = 1 -while count <= 10 do - env.out.print(count.string()) - count = count + 1 -end \ No newline at end of file + while count <= 10 do + env.out.print(count.string()) + count = count + 1 + end \ No newline at end of file diff --git a/docs/expressions/control-structures.md b/docs/expressions/control-structures.md index b79e4116..f103d8c3 100644 --- a/docs/expressions/control-structures.md +++ b/docs/expressions/control-structures.md @@ -7,7 +7,7 @@ To do real work in a program you have to be able to make decisions, iterate thro The simplest control structure is the good old `if`. It allows you to perform some action only when a condition is true. In Pony it looks like this: ```pony ---8<-- "control-structures-conditionals-if.pony" +--8<-- "control-structures-conditionals-if.pony:5:7" ``` __Can I use integers and pointers for the condition like I can in C?__ No. In Pony `if` conditions must have type `Bool`, i.e. they are always true or false. If you want to test whether a number `a` is not 0, then you need to explicitly say `a != 0`. This restriction removes a whole category of potential bugs from Pony programs. @@ -15,19 +15,19 @@ __Can I use integers and pointers for the condition like I can in C?__ No. In Po If you want some alternative code for when the condition fails just add an `else`: ```pony ---8<-- "control-structures-conditionals-if-else.pony" +--8<-- "control-structures-conditionals-if-else.pony:5:9" ``` Often you want to test more than one condition in one go, giving you more than two possible outcomes. You can nest `if` statements, but this quickly gets ugly: ```pony ---8<-- "control-structures-conditionals-nested-if-else.pony" +--8<-- "control-structures-conditionals-nested-if-else.pony:5:13" ``` As an alternative Pony provides the `elseif` keyword that combines an `else` and an `if`. This works the same as saying `else if` in other languages and you can have as many `elseif`s as you like for each `if`. ```pony ---8<-- "control-structures-conditionals-if-elseif-else.pony" +--8<-- "control-structures-conditionals-if-elseif-else.pony:5:11" ``` __Why can't I just say "else if" like I do in C? Why the extra keyword?__ The relationship between `if` and `else` in C, and other similar languages, is ambiguous. For example: @@ -52,7 +52,7 @@ In Pony control flow statements like this are all expressions that hand back a v This means you can use `if` directly in a calculation: ```pony ---8<-- "control-structures-conditionals-expressions.pony" +--8<-- "control-structures-conditionals-expressions.pony:5:5" ``` This will give __x__ a value of either 3 or 101, depending on the variable __lots__. @@ -60,13 +60,13 @@ This will give __x__ a value of either 3 or 101, depending on the variable __lot If the `then` and `else` branches of an `if` produce different types then the `if` produces a __union__ of the two. ```pony ---8<-- "control-structures-conditionals-expression-union-type.pony" +--8<-- "control-structures-conditionals-expression-union-type.pony:4:9" ``` __But what if my if doesn't have an else?__ Any `else` branch that doesn't exist gives an implicit `None`. ```pony ---8<-- "control-structures-conditionals-expression-implicit-none.pony" +--8<-- "control-structures-conditionals-expression-implicit-none.pony:4:7" ``` The same rules that apply to the value of an `if` expression applies to loops as well. Let's take a look at what a loop value would look like: @@ -102,7 +102,7 @@ Pony `while` loops are very similar to those in other languages. A condition exp Here's an example that prints out the numbers 1 to 10: ```pony ---8<-- "control-structures-loops-while.pony" +--8<-- "control-structures-loops-while.pony:3:8" ``` Just like `if` expressions, `while` is also an expression. The value returned is just the value of the expression inside the loop the last time we go round it. For this example that will be the value given by `count = count + 1` when count is incremented to 11. Since Pony assignments hand back the _old_ value our `while` loop will return 10. @@ -120,7 +120,7 @@ Sometimes you want to stop part-way through a loop and give up altogether. Pony Let's have an example. Suppose you want to go through a list of names, looking for either "Jack" or "Jill". If neither of those appear, you'll just take the last name you're given, and if you're not given any names at all, you'll use "Herbert". ```pony ---8<-- "control-structures-loops-while-break-else.pony" +--8<-- "control-structures-loops-while-break-else.pony:10:19" ``` So first we ask if there are any more names to get. If there are then we get a name and see if it's "Jack" or "Jill". If it is we're done and we break out of the loop, handing back the name we've found. If not we try again. @@ -150,7 +150,7 @@ The Pony `for` loop iterates over a collection of items using an iterator. On ea For example, to print out all the strings in an array: ```pony ---8<-- "control-structures-loops-for.pony" +--8<-- "control-structures-loops-for.pony:3:5" ``` Note the call to `values()` on the array — this is because the loop needs an iterator, not an array. @@ -166,7 +166,7 @@ where T is the type of the objects in the collection. You don't need to worry ab You can think of the above example as being equivalent to: ```pony ---8<-- "control-structures-loops-for-while-comparison.pony" +--8<-- "control-structures-loops-for-while-comparison.pony:4:8" ``` Note that the variable __name__ is declared _let_, so you cannot assign to the control variable within the loop.