Should I run code line by line?
Problems arise when I run multiple lines of the example.
2.4 Conditionals and randomness
// @@random is a class data member that is always available
// to generate random values.
// Randomly print "heads" or "tails"
if @@random.coin_toss ["Heads!"] else ["Tails!"]
// when is another way of expressing a conditional
println("Tails!") when @@random.coin_toss
// unless = "when not"
println("Heads!") unless @@random.coin_toss
This also generates a warning at compile time.
WARNING: The expression has no side effects so it does not seem useful as a statement on its own [not used as an argument or result (last expression) of a code block].
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
if @@random.coin_toss ["Heads!"] else ["Tails!"]
>>>>>>>>>>>>>>>>>>>>>> ^ <<<< Line: 1, column: 24
println("Tails!") when @@random.coin_toss
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Script code index: [23]
It works when I change it like this.
if @@random.coin_toss [print("Heads!")] else [print("Tails!")]
println("Tails!") when @@random.coin_toss
println("Heads!") unless @@random.coin_toss
["Heads!"]
to [print("Heads!")]
["Tails!")]
to [print("Tails!")]
Tutorial Workbench code is same.