I am the author of the tutorial.
I'm terribly sorry that this sample code does not work anymore (it worked with Ceylon 1.0.0, see below).
I have fixed it in the tutorial and created a runnable sample in the Ceylon Web IDE which you can use to try this out.
Basically, the problem is that, as Lucas Werkmeister pointed out, readLine() return a String?
which is equivalent to String|Null
because it may not be possible to read anything from the input (user's keyboard), in which case you get null
back.
The code sample worked with Ceylon 1.0.0 because readLine()
used to return a String
.
So for the code to compile, you need to make sure to check that what you got back exists
(ie. is NOT null
):
value userX = process.readLine();
value x = parseFloat(userX else "");
When you do userX else ""
, you tell Ceylon that if userX
exists, it should use that, if not, use ""
instead. This way, we always get a String
back...
The whole code snippet should look like this (see the sample linked to above):
process.write("Enter a number (x): ");
value userX = process.readLine();
value x = parseFloat(userX else "");
process.write("Enter a number (y): ");
value userY = process.readLine();
value y = parseFloat(userY else "");
if (exists x, exists y) {
print("``x`` * ``y`` = ``x * y``");
} else {
print("You must enter numbers!");
}
Thank you for reporting the error! Hope you enjoy the rest of the tutorial.